vport.c 13 KB

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