vport.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2007-2014 Nicira, Inc.
  4. */
  5. #include <linux/etherdevice.h>
  6. #include <linux/if.h>
  7. #include <linux/if_vlan.h>
  8. #include <linux/jhash.h>
  9. #include <linux/kernel.h>
  10. #include <linux/list.h>
  11. #include <linux/mutex.h>
  12. #include <linux/percpu.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/compat.h>
  16. #include <net/net_namespace.h>
  17. #include <linux/module.h>
  18. #include "datapath.h"
  19. #include "vport.h"
  20. #include "vport-internal_dev.h"
  21. static LIST_HEAD(vport_ops_list);
  22. /* Protected by RCU read lock for reading, ovs_mutex for writing. */
  23. static struct hlist_head *dev_table;
  24. #define VPORT_HASH_BUCKETS 1024
  25. /**
  26. * ovs_vport_init - initialize vport subsystem
  27. *
  28. * Called at module load time to initialize the vport subsystem.
  29. */
  30. int ovs_vport_init(void)
  31. {
  32. dev_table = kzalloc_objs(struct hlist_head, VPORT_HASH_BUCKETS);
  33. if (!dev_table)
  34. return -ENOMEM;
  35. return 0;
  36. }
  37. /**
  38. * ovs_vport_exit - shutdown vport subsystem
  39. *
  40. * Called at module exit time to shutdown the vport subsystem.
  41. */
  42. void ovs_vport_exit(void)
  43. {
  44. kfree(dev_table);
  45. }
  46. static struct hlist_head *hash_bucket(const struct net *net, const char *name)
  47. {
  48. unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
  49. return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
  50. }
  51. int __ovs_vport_ops_register(struct vport_ops *ops)
  52. {
  53. int err = -EEXIST;
  54. struct vport_ops *o;
  55. ovs_lock();
  56. list_for_each_entry(o, &vport_ops_list, list)
  57. if (ops->type == o->type)
  58. goto errout;
  59. list_add_tail(&ops->list, &vport_ops_list);
  60. err = 0;
  61. errout:
  62. ovs_unlock();
  63. return err;
  64. }
  65. EXPORT_SYMBOL_GPL(__ovs_vport_ops_register);
  66. void ovs_vport_ops_unregister(struct vport_ops *ops)
  67. {
  68. ovs_lock();
  69. list_del(&ops->list);
  70. ovs_unlock();
  71. }
  72. EXPORT_SYMBOL_GPL(ovs_vport_ops_unregister);
  73. /**
  74. * ovs_vport_locate - find a port that has already been created
  75. *
  76. * @net: network namespace
  77. * @name: name of port to find
  78. *
  79. * Must be called with ovs or RCU read lock.
  80. */
  81. struct vport *ovs_vport_locate(const struct net *net, const char *name)
  82. {
  83. struct hlist_head *bucket = hash_bucket(net, name);
  84. struct vport *vport;
  85. hlist_for_each_entry_rcu(vport, bucket, hash_node,
  86. lockdep_ovsl_is_held())
  87. if (!strcmp(name, ovs_vport_name(vport)) &&
  88. net_eq(ovs_dp_get_net(vport->dp), net))
  89. return vport;
  90. return NULL;
  91. }
  92. /**
  93. * ovs_vport_alloc - allocate and initialize new vport
  94. *
  95. * @priv_size: Size of private data area to allocate.
  96. * @ops: vport device ops
  97. * @parms: information about new vport.
  98. *
  99. * Allocate and initialize a new vport defined by @ops. The vport will contain
  100. * a private data area of size @priv_size that can be accessed using
  101. * vport_priv(). Some parameters of the vport will be initialized from @parms.
  102. * @vports that are no longer needed should be released with
  103. * vport_free().
  104. */
  105. struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
  106. const struct vport_parms *parms)
  107. {
  108. struct vport *vport;
  109. size_t alloc_size;
  110. int err;
  111. alloc_size = sizeof(struct vport);
  112. if (priv_size) {
  113. alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
  114. alloc_size += priv_size;
  115. }
  116. vport = kzalloc(alloc_size, GFP_KERNEL);
  117. if (!vport)
  118. return ERR_PTR(-ENOMEM);
  119. vport->upcall_stats = netdev_alloc_pcpu_stats(struct vport_upcall_stats_percpu);
  120. if (!vport->upcall_stats) {
  121. err = -ENOMEM;
  122. goto err_kfree_vport;
  123. }
  124. vport->dp = parms->dp;
  125. vport->port_no = parms->port_no;
  126. vport->ops = ops;
  127. INIT_HLIST_NODE(&vport->dp_hash_node);
  128. if (ovs_vport_set_upcall_portids(vport, parms->upcall_portids)) {
  129. err = -EINVAL;
  130. goto err_free_percpu;
  131. }
  132. return vport;
  133. err_free_percpu:
  134. free_percpu(vport->upcall_stats);
  135. err_kfree_vport:
  136. kfree(vport);
  137. return ERR_PTR(err);
  138. }
  139. EXPORT_SYMBOL_GPL(ovs_vport_alloc);
  140. /**
  141. * ovs_vport_free - uninitialize and free vport
  142. *
  143. * @vport: vport to free
  144. *
  145. * Frees a vport allocated with vport_alloc() when it is no longer needed.
  146. *
  147. * The caller must ensure that an RCU grace period has passed since the last
  148. * time @vport was in a datapath.
  149. */
  150. void ovs_vport_free(struct vport *vport)
  151. {
  152. /* vport is freed from RCU callback or error path, Therefore
  153. * it is safe to use raw dereference.
  154. */
  155. kfree(rcu_dereference_raw(vport->upcall_portids));
  156. free_percpu(vport->upcall_stats);
  157. kfree(vport);
  158. }
  159. EXPORT_SYMBOL_GPL(ovs_vport_free);
  160. static struct vport_ops *ovs_vport_lookup(const struct vport_parms *parms)
  161. {
  162. struct vport_ops *ops;
  163. list_for_each_entry(ops, &vport_ops_list, list)
  164. if (ops->type == parms->type)
  165. return ops;
  166. return NULL;
  167. }
  168. /**
  169. * ovs_vport_add - add vport device (for kernel callers)
  170. *
  171. * @parms: Information about new vport.
  172. *
  173. * Creates a new vport with the specified configuration (which is dependent on
  174. * device type). ovs_mutex must be held.
  175. */
  176. struct vport *ovs_vport_add(const struct vport_parms *parms)
  177. {
  178. struct vport_ops *ops;
  179. struct vport *vport;
  180. ops = ovs_vport_lookup(parms);
  181. if (ops) {
  182. struct hlist_head *bucket;
  183. if (!try_module_get(ops->owner))
  184. return ERR_PTR(-EAFNOSUPPORT);
  185. vport = ops->create(parms);
  186. if (IS_ERR(vport)) {
  187. module_put(ops->owner);
  188. return vport;
  189. }
  190. bucket = hash_bucket(ovs_dp_get_net(vport->dp),
  191. ovs_vport_name(vport));
  192. hlist_add_head_rcu(&vport->hash_node, bucket);
  193. return vport;
  194. }
  195. /* Unlock to attempt module load and return -EAGAIN if load
  196. * was successful as we need to restart the port addition
  197. * workflow.
  198. */
  199. ovs_unlock();
  200. request_module("vport-type-%d", parms->type);
  201. ovs_lock();
  202. if (!ovs_vport_lookup(parms))
  203. return ERR_PTR(-EAFNOSUPPORT);
  204. else
  205. return ERR_PTR(-EAGAIN);
  206. }
  207. /**
  208. * ovs_vport_set_options - modify existing vport device (for kernel callers)
  209. *
  210. * @vport: vport to modify.
  211. * @options: New configuration.
  212. *
  213. * Modifies an existing device with the specified configuration (which is
  214. * dependent on device type). ovs_mutex must be held.
  215. */
  216. int ovs_vport_set_options(struct vport *vport, struct nlattr *options)
  217. {
  218. if (!vport->ops->set_options)
  219. return -EOPNOTSUPP;
  220. return vport->ops->set_options(vport, options);
  221. }
  222. /**
  223. * ovs_vport_del - delete existing vport device
  224. *
  225. * @vport: vport to delete.
  226. *
  227. * Detaches @vport from its datapath and destroys it. ovs_mutex must
  228. * be held.
  229. */
  230. void ovs_vport_del(struct vport *vport)
  231. {
  232. hlist_del_rcu(&vport->hash_node);
  233. module_put(vport->ops->owner);
  234. vport->ops->destroy(vport);
  235. }
  236. /**
  237. * ovs_vport_get_stats - retrieve device stats
  238. *
  239. * @vport: vport from which to retrieve the stats
  240. * @stats: location to store stats
  241. *
  242. * Retrieves transmit, receive, and error stats for the given device.
  243. *
  244. * Must be called with ovs_mutex or rcu_read_lock.
  245. */
  246. void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
  247. {
  248. const struct rtnl_link_stats64 *dev_stats;
  249. struct rtnl_link_stats64 temp;
  250. dev_stats = dev_get_stats(vport->dev, &temp);
  251. stats->rx_errors = dev_stats->rx_errors;
  252. stats->tx_errors = dev_stats->tx_errors;
  253. stats->tx_dropped = dev_stats->tx_dropped;
  254. stats->rx_dropped = dev_stats->rx_dropped;
  255. stats->rx_bytes = dev_stats->rx_bytes;
  256. stats->rx_packets = dev_stats->rx_packets;
  257. stats->tx_bytes = dev_stats->tx_bytes;
  258. stats->tx_packets = dev_stats->tx_packets;
  259. }
  260. /**
  261. * ovs_vport_get_upcall_stats - retrieve upcall stats
  262. *
  263. * @vport: vport from which to retrieve the stats.
  264. * @skb: sk_buff where upcall stats should be appended.
  265. *
  266. * Retrieves upcall stats for the given device.
  267. *
  268. * Must be called with ovs_mutex or rcu_read_lock.
  269. */
  270. int ovs_vport_get_upcall_stats(struct vport *vport, struct sk_buff *skb)
  271. {
  272. u64 tx_success = 0, tx_fail = 0;
  273. struct nlattr *nla;
  274. int i;
  275. for_each_possible_cpu(i) {
  276. const struct vport_upcall_stats_percpu *stats;
  277. u64 n_success, n_fail;
  278. unsigned int start;
  279. stats = per_cpu_ptr(vport->upcall_stats, i);
  280. do {
  281. start = u64_stats_fetch_begin(&stats->syncp);
  282. n_success = u64_stats_read(&stats->n_success);
  283. n_fail = u64_stats_read(&stats->n_fail);
  284. } while (u64_stats_fetch_retry(&stats->syncp, start));
  285. tx_success += n_success;
  286. tx_fail += n_fail;
  287. }
  288. nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_UPCALL_STATS);
  289. if (!nla)
  290. return -EMSGSIZE;
  291. if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_SUCCESS, tx_success,
  292. OVS_VPORT_ATTR_PAD)) {
  293. nla_nest_cancel(skb, nla);
  294. return -EMSGSIZE;
  295. }
  296. if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_FAIL, tx_fail,
  297. OVS_VPORT_ATTR_PAD)) {
  298. nla_nest_cancel(skb, nla);
  299. return -EMSGSIZE;
  300. }
  301. nla_nest_end(skb, nla);
  302. return 0;
  303. }
  304. /**
  305. * ovs_vport_get_options - retrieve device options
  306. *
  307. * @vport: vport from which to retrieve the options.
  308. * @skb: sk_buff where options should be appended.
  309. *
  310. * Retrieves the configuration of the given device, appending an
  311. * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
  312. * vport-specific attributes to @skb.
  313. *
  314. * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
  315. * negative error code if a real error occurred. If an error occurs, @skb is
  316. * left unmodified.
  317. *
  318. * Must be called with ovs_mutex or rcu_read_lock.
  319. */
  320. int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb)
  321. {
  322. struct nlattr *nla;
  323. int err;
  324. if (!vport->ops->get_options)
  325. return 0;
  326. nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_OPTIONS);
  327. if (!nla)
  328. return -EMSGSIZE;
  329. err = vport->ops->get_options(vport, skb);
  330. if (err) {
  331. nla_nest_cancel(skb, nla);
  332. return err;
  333. }
  334. nla_nest_end(skb, nla);
  335. return 0;
  336. }
  337. /**
  338. * ovs_vport_set_upcall_portids - set upcall portids of @vport.
  339. *
  340. * @vport: vport to modify.
  341. * @ids: new configuration, an array of port ids.
  342. *
  343. * Sets the vport's upcall_portids to @ids.
  344. *
  345. * Returns 0 if successful, -EINVAL if @ids is zero length or cannot be parsed
  346. * as an array of U32.
  347. *
  348. * Must be called with ovs_mutex.
  349. */
  350. int ovs_vport_set_upcall_portids(struct vport *vport, const struct nlattr *ids)
  351. {
  352. struct vport_portids *old, *vport_portids;
  353. if (!nla_len(ids) || nla_len(ids) % sizeof(u32))
  354. return -EINVAL;
  355. old = ovsl_dereference(vport->upcall_portids);
  356. vport_portids = kmalloc(sizeof(*vport_portids) + nla_len(ids),
  357. GFP_KERNEL);
  358. if (!vport_portids)
  359. return -ENOMEM;
  360. vport_portids->n_ids = nla_len(ids) / sizeof(u32);
  361. vport_portids->rn_ids = reciprocal_value(vport_portids->n_ids);
  362. nla_memcpy(vport_portids->ids, ids, nla_len(ids));
  363. rcu_assign_pointer(vport->upcall_portids, vport_portids);
  364. if (old)
  365. kfree_rcu(old, rcu);
  366. return 0;
  367. }
  368. /**
  369. * ovs_vport_get_upcall_portids - get the upcall_portids of @vport.
  370. *
  371. * @vport: vport from which to retrieve the portids.
  372. * @skb: sk_buff where portids should be appended.
  373. *
  374. * Retrieves the configuration of the given vport, appending the
  375. * %OVS_VPORT_ATTR_UPCALL_PID attribute which is the array of upcall
  376. * portids to @skb.
  377. *
  378. * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room.
  379. * If an error occurs, @skb is left unmodified. Must be called with
  380. * ovs_mutex or rcu_read_lock.
  381. */
  382. int ovs_vport_get_upcall_portids(const struct vport *vport,
  383. struct sk_buff *skb)
  384. {
  385. struct vport_portids *ids;
  386. ids = rcu_dereference_ovsl(vport->upcall_portids);
  387. if (vport->dp->user_features & OVS_DP_F_VPORT_PIDS)
  388. return nla_put(skb, OVS_VPORT_ATTR_UPCALL_PID,
  389. ids->n_ids * sizeof(u32), (void *)ids->ids);
  390. else
  391. return nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, ids->ids[0]);
  392. }
  393. /**
  394. * ovs_vport_find_upcall_portid - find the upcall portid to send upcall.
  395. *
  396. * @vport: vport from which the missed packet is received.
  397. * @skb: skb that the missed packet was received.
  398. *
  399. * Uses the skb_get_hash() to select the upcall portid to send the
  400. * upcall.
  401. *
  402. * Returns the portid of the target socket. Must be called with rcu_read_lock.
  403. */
  404. u32 ovs_vport_find_upcall_portid(const struct vport *vport,
  405. struct sk_buff *skb)
  406. {
  407. struct vport_portids *ids;
  408. u32 ids_index;
  409. u32 hash;
  410. ids = rcu_dereference(vport->upcall_portids);
  411. /* If there is only one portid, select it in the fast-path. */
  412. if (ids->n_ids == 1)
  413. return ids->ids[0];
  414. hash = skb_get_hash(skb);
  415. ids_index = hash - ids->n_ids * reciprocal_divide(hash, ids->rn_ids);
  416. return ids->ids[ids_index];
  417. }
  418. /**
  419. * ovs_vport_receive - pass up received packet to the datapath for processing
  420. *
  421. * @vport: vport that received the packet
  422. * @skb: skb that was received
  423. * @tun_info: tunnel (if any) that carried packet
  424. *
  425. * Must be called with rcu_read_lock. The packet cannot be shared and
  426. * skb->data should point to the Ethernet header.
  427. */
  428. int ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
  429. const struct ip_tunnel_info *tun_info)
  430. {
  431. struct sw_flow_key key;
  432. int error;
  433. OVS_CB(skb)->input_vport = vport;
  434. OVS_CB(skb)->mru = 0;
  435. OVS_CB(skb)->cutlen = 0;
  436. OVS_CB(skb)->probability = 0;
  437. OVS_CB(skb)->upcall_pid = 0;
  438. if (unlikely(dev_net(skb->dev) != ovs_dp_get_net(vport->dp))) {
  439. u32 mark;
  440. mark = skb->mark;
  441. skb_scrub_packet(skb, true);
  442. skb->mark = mark;
  443. tun_info = NULL;
  444. }
  445. /* Extract flow from 'skb' into 'key'. */
  446. error = ovs_flow_key_extract(tun_info, skb, &key);
  447. if (unlikely(error)) {
  448. kfree_skb(skb);
  449. return error;
  450. }
  451. ovs_dp_process_packet(skb, &key);
  452. return 0;
  453. }
  454. static int packet_length(const struct sk_buff *skb,
  455. struct net_device *dev)
  456. {
  457. int length = skb->len - dev->hard_header_len;
  458. if (!skb_vlan_tag_present(skb) &&
  459. eth_type_vlan(skb->protocol))
  460. length -= VLAN_HLEN;
  461. /* Don't subtract for multiple VLAN tags. Most (all?) drivers allow
  462. * (ETH_LEN + VLAN_HLEN) in addition to the mtu value, but almost none
  463. * account for 802.1ad. e.g. is_skb_forwardable().
  464. */
  465. return length > 0 ? length : 0;
  466. }
  467. void ovs_vport_send(struct vport *vport, struct sk_buff *skb, u8 mac_proto)
  468. {
  469. int mtu = vport->dev->mtu;
  470. switch (vport->dev->type) {
  471. case ARPHRD_NONE:
  472. if (mac_proto == MAC_PROTO_ETHERNET) {
  473. skb_reset_network_header(skb);
  474. skb_reset_mac_len(skb);
  475. skb->protocol = htons(ETH_P_TEB);
  476. } else if (mac_proto != MAC_PROTO_NONE) {
  477. WARN_ON_ONCE(1);
  478. goto drop;
  479. }
  480. break;
  481. case ARPHRD_ETHER:
  482. if (mac_proto != MAC_PROTO_ETHERNET)
  483. goto drop;
  484. break;
  485. default:
  486. goto drop;
  487. }
  488. if (unlikely(packet_length(skb, vport->dev) > mtu &&
  489. !skb_is_gso(skb))) {
  490. vport->dev->stats.tx_errors++;
  491. if (vport->dev->flags & IFF_UP)
  492. net_warn_ratelimited("%s: dropped over-mtu packet: "
  493. "%d > %d\n", vport->dev->name,
  494. packet_length(skb, vport->dev),
  495. mtu);
  496. goto drop;
  497. }
  498. skb->dev = vport->dev;
  499. skb_clear_tstamp(skb);
  500. vport->ops->send(skb);
  501. return;
  502. drop:
  503. kfree_skb(skb);
  504. }