vport.c 10 KB

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