vport.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (c) 2007-2012 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 "datapath.h"
  31. #include "vport.h"
  32. #include "vport-internal_dev.h"
  33. /* List of statically compiled vport implementations. Don't forget to also
  34. * add yours to the list at the bottom of vport.h. */
  35. static const struct vport_ops *vport_ops_list[] = {
  36. &ovs_netdev_vport_ops,
  37. &ovs_internal_vport_ops,
  38. #ifdef CONFIG_OPENVSWITCH_GRE
  39. &ovs_gre_vport_ops,
  40. #endif
  41. #ifdef CONFIG_OPENVSWITCH_VXLAN
  42. &ovs_vxlan_vport_ops,
  43. #endif
  44. };
  45. /* Protected by RCU read lock for reading, ovs_mutex for writing. */
  46. static struct hlist_head *dev_table;
  47. #define VPORT_HASH_BUCKETS 1024
  48. /**
  49. * ovs_vport_init - initialize vport subsystem
  50. *
  51. * Called at module load time to initialize the vport subsystem.
  52. */
  53. int ovs_vport_init(void)
  54. {
  55. dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  56. GFP_KERNEL);
  57. if (!dev_table)
  58. return -ENOMEM;
  59. return 0;
  60. }
  61. /**
  62. * ovs_vport_exit - shutdown vport subsystem
  63. *
  64. * Called at module exit time to shutdown the vport subsystem.
  65. */
  66. void ovs_vport_exit(void)
  67. {
  68. kfree(dev_table);
  69. }
  70. static struct hlist_head *hash_bucket(struct net *net, const char *name)
  71. {
  72. unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
  73. return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
  74. }
  75. /**
  76. * ovs_vport_locate - find a port that has already been created
  77. *
  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(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. if (!strcmp(name, vport->ops->get_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. *
  98. * Allocate and initialize a new vport defined by @ops. The vport will contain
  99. * a private data area of size @priv_size that can be accessed using
  100. * vport_priv(). vports that are no longer needed should be released with
  101. * vport_free().
  102. */
  103. struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
  104. const struct vport_parms *parms)
  105. {
  106. struct vport *vport;
  107. size_t alloc_size;
  108. int i;
  109. alloc_size = sizeof(struct vport);
  110. if (priv_size) {
  111. alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
  112. alloc_size += priv_size;
  113. }
  114. vport = kzalloc(alloc_size, GFP_KERNEL);
  115. if (!vport)
  116. return ERR_PTR(-ENOMEM);
  117. vport->dp = parms->dp;
  118. vport->port_no = parms->port_no;
  119. vport->upcall_portid = parms->upcall_portid;
  120. vport->ops = ops;
  121. INIT_HLIST_NODE(&vport->dp_hash_node);
  122. vport->percpu_stats = alloc_percpu(struct pcpu_tstats);
  123. if (!vport->percpu_stats) {
  124. kfree(vport);
  125. return ERR_PTR(-ENOMEM);
  126. }
  127. for_each_possible_cpu(i) {
  128. struct pcpu_tstats *vport_stats;
  129. vport_stats = per_cpu_ptr(vport->percpu_stats, i);
  130. u64_stats_init(&vport_stats->syncp);
  131. }
  132. spin_lock_init(&vport->stats_lock);
  133. return vport;
  134. }
  135. /**
  136. * ovs_vport_free - uninitialize and free vport
  137. *
  138. * @vport: vport to free
  139. *
  140. * Frees a vport allocated with vport_alloc() when it is no longer needed.
  141. *
  142. * The caller must ensure that an RCU grace period has passed since the last
  143. * time @vport was in a datapath.
  144. */
  145. void ovs_vport_free(struct vport *vport)
  146. {
  147. free_percpu(vport->percpu_stats);
  148. kfree(vport);
  149. }
  150. /**
  151. * ovs_vport_add - add vport device (for kernel callers)
  152. *
  153. * @parms: Information about new vport.
  154. *
  155. * Creates a new vport with the specified configuration (which is dependent on
  156. * device type). ovs_mutex must be held.
  157. */
  158. struct vport *ovs_vport_add(const struct vport_parms *parms)
  159. {
  160. struct vport *vport;
  161. int err = 0;
  162. int i;
  163. for (i = 0; i < ARRAY_SIZE(vport_ops_list); i++) {
  164. if (vport_ops_list[i]->type == parms->type) {
  165. struct hlist_head *bucket;
  166. vport = vport_ops_list[i]->create(parms);
  167. if (IS_ERR(vport)) {
  168. err = PTR_ERR(vport);
  169. goto out;
  170. }
  171. bucket = hash_bucket(ovs_dp_get_net(vport->dp),
  172. vport->ops->get_name(vport));
  173. hlist_add_head_rcu(&vport->hash_node, bucket);
  174. return vport;
  175. }
  176. }
  177. err = -EAFNOSUPPORT;
  178. out:
  179. return ERR_PTR(err);
  180. }
  181. /**
  182. * ovs_vport_set_options - modify existing vport device (for kernel callers)
  183. *
  184. * @vport: vport to modify.
  185. * @options: New configuration.
  186. *
  187. * Modifies an existing device with the specified configuration (which is
  188. * dependent on device type). ovs_mutex must be held.
  189. */
  190. int ovs_vport_set_options(struct vport *vport, struct nlattr *options)
  191. {
  192. if (!vport->ops->set_options)
  193. return -EOPNOTSUPP;
  194. return vport->ops->set_options(vport, options);
  195. }
  196. /**
  197. * ovs_vport_del - delete existing vport device
  198. *
  199. * @vport: vport to delete.
  200. *
  201. * Detaches @vport from its datapath and destroys it. It is possible to fail
  202. * for reasons such as lack of memory. ovs_mutex must be held.
  203. */
  204. void ovs_vport_del(struct vport *vport)
  205. {
  206. ASSERT_OVSL();
  207. hlist_del_rcu(&vport->hash_node);
  208. vport->ops->destroy(vport);
  209. }
  210. /**
  211. * ovs_vport_get_stats - retrieve device stats
  212. *
  213. * @vport: vport from which to retrieve the stats
  214. * @stats: location to store stats
  215. *
  216. * Retrieves transmit, receive, and error stats for the given device.
  217. *
  218. * Must be called with ovs_mutex or rcu_read_lock.
  219. */
  220. void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
  221. {
  222. int i;
  223. memset(stats, 0, sizeof(*stats));
  224. /* We potentially have 2 sources of stats that need to be combined:
  225. * those we have collected (split into err_stats and percpu_stats) from
  226. * set_stats() and device error stats from netdev->get_stats() (for
  227. * errors that happen downstream and therefore aren't reported through
  228. * our vport_record_error() function).
  229. * Stats from first source are reported by ovs (OVS_VPORT_ATTR_STATS).
  230. * netdev-stats can be directly read over netlink-ioctl.
  231. */
  232. spin_lock_bh(&vport->stats_lock);
  233. stats->rx_errors = vport->err_stats.rx_errors;
  234. stats->tx_errors = vport->err_stats.tx_errors;
  235. stats->tx_dropped = vport->err_stats.tx_dropped;
  236. stats->rx_dropped = vport->err_stats.rx_dropped;
  237. spin_unlock_bh(&vport->stats_lock);
  238. for_each_possible_cpu(i) {
  239. const struct pcpu_tstats *percpu_stats;
  240. struct pcpu_tstats local_stats;
  241. unsigned int start;
  242. percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
  243. do {
  244. start = u64_stats_fetch_begin_bh(&percpu_stats->syncp);
  245. local_stats = *percpu_stats;
  246. } while (u64_stats_fetch_retry_bh(&percpu_stats->syncp, start));
  247. stats->rx_bytes += local_stats.rx_bytes;
  248. stats->rx_packets += local_stats.rx_packets;
  249. stats->tx_bytes += local_stats.tx_bytes;
  250. stats->tx_packets += local_stats.tx_packets;
  251. }
  252. }
  253. /**
  254. * ovs_vport_get_options - retrieve device options
  255. *
  256. * @vport: vport from which to retrieve the options.
  257. * @skb: sk_buff where options should be appended.
  258. *
  259. * Retrieves the configuration of the given device, appending an
  260. * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
  261. * vport-specific attributes to @skb.
  262. *
  263. * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
  264. * negative error code if a real error occurred. If an error occurs, @skb is
  265. * left unmodified.
  266. *
  267. * Must be called with ovs_mutex or rcu_read_lock.
  268. */
  269. int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb)
  270. {
  271. struct nlattr *nla;
  272. int err;
  273. if (!vport->ops->get_options)
  274. return 0;
  275. nla = nla_nest_start(skb, OVS_VPORT_ATTR_OPTIONS);
  276. if (!nla)
  277. return -EMSGSIZE;
  278. err = vport->ops->get_options(vport, skb);
  279. if (err) {
  280. nla_nest_cancel(skb, nla);
  281. return err;
  282. }
  283. nla_nest_end(skb, nla);
  284. return 0;
  285. }
  286. /**
  287. * ovs_vport_receive - pass up received packet to the datapath for processing
  288. *
  289. * @vport: vport that received the packet
  290. * @skb: skb that was received
  291. * @tun_key: tunnel (if any) that carried packet
  292. *
  293. * Must be called with rcu_read_lock. The packet cannot be shared and
  294. * skb->data should point to the Ethernet header.
  295. */
  296. void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
  297. struct ovs_key_ipv4_tunnel *tun_key)
  298. {
  299. struct pcpu_tstats *stats;
  300. stats = this_cpu_ptr(vport->percpu_stats);
  301. u64_stats_update_begin(&stats->syncp);
  302. stats->rx_packets++;
  303. stats->rx_bytes += skb->len;
  304. u64_stats_update_end(&stats->syncp);
  305. OVS_CB(skb)->tun_key = tun_key;
  306. ovs_dp_process_received_packet(vport, skb);
  307. }
  308. /**
  309. * ovs_vport_send - send a packet on a device
  310. *
  311. * @vport: vport on which to send the packet
  312. * @skb: skb to send
  313. *
  314. * Sends the given packet and returns the length of data sent. Either ovs
  315. * lock or rcu_read_lock must be held.
  316. */
  317. int ovs_vport_send(struct vport *vport, struct sk_buff *skb)
  318. {
  319. int sent = vport->ops->send(vport, skb);
  320. if (likely(sent > 0)) {
  321. struct pcpu_tstats *stats;
  322. stats = this_cpu_ptr(vport->percpu_stats);
  323. u64_stats_update_begin(&stats->syncp);
  324. stats->tx_packets++;
  325. stats->tx_bytes += sent;
  326. u64_stats_update_end(&stats->syncp);
  327. } else if (sent < 0) {
  328. ovs_vport_record_error(vport, VPORT_E_TX_ERROR);
  329. kfree_skb(skb);
  330. } else
  331. ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
  332. return sent;
  333. }
  334. /**
  335. * ovs_vport_record_error - indicate device error to generic stats layer
  336. *
  337. * @vport: vport that encountered the error
  338. * @err_type: one of enum vport_err_type types to indicate the error type
  339. *
  340. * If using the vport generic stats layer indicate that an error of the given
  341. * type has occurred.
  342. */
  343. void ovs_vport_record_error(struct vport *vport, enum vport_err_type err_type)
  344. {
  345. spin_lock(&vport->stats_lock);
  346. switch (err_type) {
  347. case VPORT_E_RX_DROPPED:
  348. vport->err_stats.rx_dropped++;
  349. break;
  350. case VPORT_E_RX_ERROR:
  351. vport->err_stats.rx_errors++;
  352. break;
  353. case VPORT_E_TX_DROPPED:
  354. vport->err_stats.tx_dropped++;
  355. break;
  356. case VPORT_E_TX_ERROR:
  357. vport->err_stats.tx_errors++;
  358. break;
  359. }
  360. spin_unlock(&vport->stats_lock);
  361. }
  362. static void free_vport_rcu(struct rcu_head *rcu)
  363. {
  364. struct vport *vport = container_of(rcu, struct vport, rcu);
  365. ovs_vport_free(vport);
  366. }
  367. void ovs_vport_deferred_free(struct vport *vport)
  368. {
  369. if (!vport)
  370. return;
  371. call_rcu(&vport->rcu, free_vport_rcu);
  372. }