vport.c 13 KB

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