openvswitch: Remove Linux bridge compatibility.
[sliver-openvswitch.git] / datapath / vport.c
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
19 #include <linux/etherdevice.h>
20 #include <linux/if.h>
21 #include <linux/if_vlan.h>
22 #include <linux/jhash.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/percpu.h>
27 #include <linux/rcupdate.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/compat.h>
30 #include <linux/version.h>
31 #include <net/net_namespace.h>
32
33 #include "datapath.h"
34 #include "vport.h"
35 #include "vport-internal_dev.h"
36
37 /* List of statically compiled vport implementations.  Don't forget to also
38  * add yours to the list at the bottom of vport.h. */
39 static const struct vport_ops *base_vport_ops_list[] = {
40         &ovs_netdev_vport_ops,
41         &ovs_internal_vport_ops,
42         &ovs_patch_vport_ops,
43         &ovs_gre_vport_ops,
44         &ovs_gre_ft_vport_ops,
45         &ovs_gre64_vport_ops,
46 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
47         &ovs_capwap_vport_ops,
48         &ovs_vxlan_vport_ops,
49 #endif
50 };
51
52 static const struct vport_ops **vport_ops_list;
53 static int n_vport_types;
54
55 /* Protected by RCU read lock for reading, RTNL lock for writing. */
56 static struct hlist_head *dev_table;
57 #define VPORT_HASH_BUCKETS 1024
58
59 /**
60  *      ovs_vport_init - initialize vport subsystem
61  *
62  * Called at module load time to initialize the vport subsystem and any
63  * compiled in vport types.
64  */
65 int ovs_vport_init(void)
66 {
67         int err;
68         int i;
69
70         dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
71                             GFP_KERNEL);
72         if (!dev_table) {
73                 err = -ENOMEM;
74                 goto error;
75         }
76
77         vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
78                                  sizeof(struct vport_ops *), GFP_KERNEL);
79         if (!vport_ops_list) {
80                 err = -ENOMEM;
81                 goto error_dev_table;
82         }
83
84         for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
85                 const struct vport_ops *new_ops = base_vport_ops_list[i];
86
87                 if (new_ops->init)
88                         err = new_ops->init();
89                 else
90                         err = 0;
91
92                 if (!err)
93                         vport_ops_list[n_vport_types++] = new_ops;
94                 else if (new_ops->flags & VPORT_F_REQUIRED) {
95                         ovs_vport_exit();
96                         goto error;
97                 }
98         }
99
100         return 0;
101
102 error_dev_table:
103         kfree(dev_table);
104 error:
105         return err;
106 }
107
108 /**
109  *      ovs_vport_exit - shutdown vport subsystem
110  *
111  * Called at module exit time to shutdown the vport subsystem and any
112  * initialized vport types.
113  */
114 void ovs_vport_exit(void)
115 {
116         int i;
117
118         for (i = 0; i < n_vport_types; i++) {
119                 if (vport_ops_list[i]->exit)
120                         vport_ops_list[i]->exit();
121         }
122
123         kfree(vport_ops_list);
124         kfree(dev_table);
125 }
126
127 static struct hlist_head *hash_bucket(struct net *net, const char *name)
128 {
129         unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
130         return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
131 }
132
133 /**
134  *      ovs_vport_locate - find a port that has already been created
135  *
136  * @name: name of port to find
137  *
138  * Must be called with RTNL or RCU read lock.
139  */
140 struct vport *ovs_vport_locate(struct net *net, const char *name)
141 {
142         struct hlist_head *bucket = hash_bucket(net, name);
143         struct vport *vport;
144         struct hlist_node *node;
145
146         hlist_for_each_entry_rcu(vport, node, bucket, hash_node)
147                 if (!strcmp(name, vport->ops->get_name(vport)) &&
148                     net_eq(ovs_dp_get_net(vport->dp), net))
149                         return vport;
150
151         return NULL;
152 }
153
154 /**
155  *      ovs_vport_alloc - allocate and initialize new vport
156  *
157  * @priv_size: Size of private data area to allocate.
158  * @ops: vport device ops
159  *
160  * Allocate and initialize a new vport defined by @ops.  The vport will contain
161  * a private data area of size @priv_size that can be accessed using
162  * vport_priv().  vports that are no longer needed should be released with
163  * ovs_vport_free().
164  */
165 struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
166                               const struct vport_parms *parms)
167 {
168         struct vport *vport;
169         size_t alloc_size;
170
171         alloc_size = sizeof(struct vport);
172         if (priv_size) {
173                 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
174                 alloc_size += priv_size;
175         }
176
177         vport = kzalloc(alloc_size, GFP_KERNEL);
178         if (!vport)
179                 return ERR_PTR(-ENOMEM);
180
181         vport->dp = parms->dp;
182         vport->port_no = parms->port_no;
183         vport->upcall_portid = parms->upcall_portid;
184         vport->ops = ops;
185         INIT_HLIST_NODE(&vport->dp_hash_node);
186
187         vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
188         if (!vport->percpu_stats) {
189                 kfree(vport);
190                 return ERR_PTR(-ENOMEM);
191         }
192
193         spin_lock_init(&vport->stats_lock);
194
195         return vport;
196 }
197
198 /**
199  *      ovs_vport_free - uninitialize and free vport
200  *
201  * @vport: vport to free
202  *
203  * Frees a vport allocated with ovs_vport_alloc() when it is no longer needed.
204  *
205  * The caller must ensure that an RCU grace period has passed since the last
206  * time @vport was in a datapath.
207  */
208 void ovs_vport_free(struct vport *vport)
209 {
210         free_percpu(vport->percpu_stats);
211         kfree(vport);
212 }
213
214 /**
215  *      ovs_vport_add - add vport device (for kernel callers)
216  *
217  * @parms: Information about new vport.
218  *
219  * Creates a new vport with the specified configuration (which is dependent on
220  * device type).  RTNL lock must be held.
221  */
222 struct vport *ovs_vport_add(const struct vport_parms *parms)
223 {
224         struct vport *vport;
225         int err = 0;
226         int i;
227
228         ASSERT_RTNL();
229
230         for (i = 0; i < n_vport_types; i++) {
231                 if (vport_ops_list[i]->type == parms->type) {
232                         struct hlist_head *bucket;
233
234                         vport = vport_ops_list[i]->create(parms);
235                         if (IS_ERR(vport)) {
236                                 err = PTR_ERR(vport);
237                                 goto out;
238                         }
239
240                         bucket = hash_bucket(ovs_dp_get_net(vport->dp),
241                                              vport->ops->get_name(vport));
242                         hlist_add_head_rcu(&vport->hash_node, bucket);
243                         return vport;
244                 }
245         }
246
247         err = -EAFNOSUPPORT;
248
249 out:
250         return ERR_PTR(err);
251 }
252
253 /**
254  *      ovs_vport_set_options - modify existing vport device (for kernel callers)
255  *
256  * @vport: vport to modify.
257  * @port: New configuration.
258  *
259  * Modifies an existing device with the specified configuration (which is
260  * dependent on device type).  RTNL lock must be held.
261  */
262 int ovs_vport_set_options(struct vport *vport, struct nlattr *options)
263 {
264         ASSERT_RTNL();
265
266         if (!vport->ops->set_options)
267                 return -EOPNOTSUPP;
268         return vport->ops->set_options(vport, options);
269 }
270
271 /**
272  *      ovs_vport_del - delete existing vport device
273  *
274  * @vport: vport to delete.
275  *
276  * Detaches @vport from its datapath and destroys it.  It is possible to fail
277  * for reasons such as lack of memory.  RTNL lock must be held.
278  */
279 void ovs_vport_del(struct vport *vport)
280 {
281         ASSERT_RTNL();
282
283         hlist_del_rcu(&vport->hash_node);
284
285         vport->ops->destroy(vport);
286 }
287
288 /**
289  *      ovs_vport_set_addr - set device Ethernet address (for kernel callers)
290  *
291  * @vport: vport on which to set Ethernet address.
292  * @addr: New address.
293  *
294  * Sets the Ethernet address of the given device.  Some devices may not support
295  * setting the Ethernet address, in which case the result will always be
296  * -EOPNOTSUPP.  RTNL lock must be held.
297  */
298 int ovs_vport_set_addr(struct vport *vport, const unsigned char *addr)
299 {
300         ASSERT_RTNL();
301
302         if (!is_valid_ether_addr(addr))
303                 return -EADDRNOTAVAIL;
304
305         if (vport->ops->set_addr)
306                 return vport->ops->set_addr(vport, addr);
307         else
308                 return -EOPNOTSUPP;
309 }
310
311 /**
312  *      ovs_vport_set_stats - sets offset device stats
313  *
314  * @vport: vport on which to set stats
315  * @stats: stats to set
316  *
317  * Provides a set of transmit, receive, and error stats to be added as an
318  * offset to the collect data when stats are retreived.  Some devices may not
319  * support setting the stats, in which case the result will always be
320  * -EOPNOTSUPP.
321  *
322  * Must be called with RTNL lock.
323  */
324 void ovs_vport_set_stats(struct vport *vport, struct ovs_vport_stats *stats)
325 {
326         ASSERT_RTNL();
327
328         spin_lock_bh(&vport->stats_lock);
329         vport->offset_stats = *stats;
330         spin_unlock_bh(&vport->stats_lock);
331 }
332
333 /**
334  *      ovs_vport_get_stats - retrieve device stats
335  *
336  * @vport: vport from which to retrieve the stats
337  * @stats: location to store stats
338  *
339  * Retrieves transmit, receive, and error stats for the given device.
340  *
341  * Must be called with RTNL lock or rcu_read_lock.
342  */
343 void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
344 {
345         int i;
346
347         /* We potentially have 3 sources of stats that need to be
348          * combined: those we have collected (split into err_stats and
349          * percpu_stats), offset_stats from set_stats(), and device
350          * error stats from netdev->get_stats() (for errors that happen
351          * downstream and therefore aren't reported through our
352          * vport_record_error() function).
353          * Stats from first two sources are merged and reported by ovs over
354          * OVS_VPORT_ATTR_STATS.
355          * netdev-stats can be directly read over netlink-ioctl.
356          */
357
358         spin_lock_bh(&vport->stats_lock);
359
360         *stats = vport->offset_stats;
361
362         stats->rx_errors        += vport->err_stats.rx_errors;
363         stats->tx_errors        += vport->err_stats.tx_errors;
364         stats->tx_dropped       += vport->err_stats.tx_dropped;
365         stats->rx_dropped       += vport->err_stats.rx_dropped;
366
367         spin_unlock_bh(&vport->stats_lock);
368
369         for_each_possible_cpu(i) {
370                 const struct vport_percpu_stats *percpu_stats;
371                 struct vport_percpu_stats local_stats;
372                 unsigned int start;
373
374                 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
375
376                 do {
377                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
378                         local_stats = *percpu_stats;
379                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
380
381                 stats->rx_bytes         += local_stats.rx_bytes;
382                 stats->rx_packets       += local_stats.rx_packets;
383                 stats->tx_bytes         += local_stats.tx_bytes;
384                 stats->tx_packets       += local_stats.tx_packets;
385         }
386 }
387
388 /**
389  *      ovs_vport_get_options - retrieve device options
390  *
391  * @vport: vport from which to retrieve the options.
392  * @skb: sk_buff where options should be appended.
393  *
394  * Retrieves the configuration of the given device, appending an
395  * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
396  * vport-specific attributes to @skb.
397  *
398  * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
399  * negative error code if a real error occurred.  If an error occurs, @skb is
400  * left unmodified.
401  *
402  * Must be called with RTNL lock or rcu_read_lock.
403  */
404 int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb)
405 {
406         struct nlattr *nla;
407
408         nla = nla_nest_start(skb, OVS_VPORT_ATTR_OPTIONS);
409         if (!nla)
410                 return -EMSGSIZE;
411
412         if (vport->ops->get_options) {
413                 int err = vport->ops->get_options(vport, skb);
414                 if (err) {
415                         nla_nest_cancel(skb, nla);
416                         return err;
417                 }
418         }
419
420         nla_nest_end(skb, nla);
421         return 0;
422 }
423
424 /**
425  *      ovs_vport_receive - pass up received packet to the datapath for processing
426  *
427  * @vport: vport that received the packet
428  * @skb: skb that was received
429  *
430  * Must be called with rcu_read_lock.  The packet cannot be shared and
431  * skb->data should point to the Ethernet header.  The caller must have already
432  * called compute_ip_summed() to initialize the checksumming fields.
433  */
434 void ovs_vport_receive(struct vport *vport, struct sk_buff *skb)
435 {
436         struct vport_percpu_stats *stats;
437
438         stats = this_cpu_ptr(vport->percpu_stats);
439         u64_stats_update_begin(&stats->sync);
440         stats->rx_packets++;
441         stats->rx_bytes += skb->len;
442         u64_stats_update_end(&stats->sync);
443
444         if (!(vport->ops->flags & VPORT_F_FLOW))
445                 OVS_CB(skb)->flow = NULL;
446
447         if (!(vport->ops->flags & VPORT_F_TUN_ID))
448                 OVS_CB(skb)->tun_key = NULL;
449
450         ovs_dp_process_received_packet(vport, skb);
451 }
452
453 /**
454  *      ovs_vport_send - send a packet on a device
455  *
456  * @vport: vport on which to send the packet
457  * @skb: skb to send
458  *
459  * Sends the given packet and returns the length of data sent.  Either RTNL
460  * lock or rcu_read_lock must be held.
461  */
462 int ovs_vport_send(struct vport *vport, struct sk_buff *skb)
463 {
464         int sent = vport->ops->send(vport, skb);
465
466         if (likely(sent)) {
467                 struct vport_percpu_stats *stats;
468
469                 stats = this_cpu_ptr(vport->percpu_stats);
470
471                 u64_stats_update_begin(&stats->sync);
472                 stats->tx_packets++;
473                 stats->tx_bytes += sent;
474                 u64_stats_update_end(&stats->sync);
475         }
476         return sent;
477 }
478
479 /**
480  *      ovs_vport_record_error - indicate device error to generic stats layer
481  *
482  * @vport: vport that encountered the error
483  * @err_type: one of enum vport_err_type types to indicate the error type
484  *
485  * If using the vport generic stats layer indicate that an error of the given
486  * type has occured.
487  */
488 void ovs_vport_record_error(struct vport *vport, enum vport_err_type err_type)
489 {
490         spin_lock(&vport->stats_lock);
491
492         switch (err_type) {
493         case VPORT_E_RX_DROPPED:
494                 vport->err_stats.rx_dropped++;
495                 break;
496
497         case VPORT_E_RX_ERROR:
498                 vport->err_stats.rx_errors++;
499                 break;
500
501         case VPORT_E_TX_DROPPED:
502                 vport->err_stats.tx_dropped++;
503                 break;
504
505         case VPORT_E_TX_ERROR:
506                 vport->err_stats.tx_errors++;
507                 break;
508         }
509
510         spin_unlock(&vport->stats_lock);
511 }