vport: Use DEFINE_PER_CPU instead of dynamically allocating loop counter.
[sliver-openvswitch.git] / datapath / vport.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include <linux/dcache.h>
10 #include <linux/etherdevice.h>
11 #include <linux/if.h>
12 #include <linux/if_vlan.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/percpu.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/compat.h>
19
20 #include "vport.h"
21 #include "vport-internal_dev.h"
22
23 /* List of statically compiled vport implementations.  Don't forget to also
24  * add yours to the list at the bottom of vport.h. */
25 static struct vport_ops *base_vport_ops_list[] = {
26         &netdev_vport_ops,
27         &internal_vport_ops,
28         &patch_vport_ops,
29         &gre_vport_ops,
30 };
31
32 static const struct vport_ops **vport_ops_list;
33 static int n_vport_types;
34
35 static struct hlist_head *dev_table;
36 #define VPORT_HASH_BUCKETS 1024
37
38 /* We limit the number of times that we pass through vport_send() to
39  * avoid blowing out the stack in the event that we have a loop. There is
40  * a separate counter for each CPU for both interrupt and non-interrupt
41  * context in order to keep the limit deterministic for a given packet. */
42 struct percpu_loop_counter {
43         int count[2];
44 };
45
46 static DEFINE_PER_CPU(struct percpu_loop_counter, vport_loop_counter);
47 #define VPORT_MAX_LOOPS 5
48
49 /* Both RTNL lock and vport_mutex need to be held when updating dev_table.
50  *
51  * If you use vport_locate and then perform some operations, you need to hold
52  * one of these locks if you don't want the vport to be deleted out from under
53  * you.
54  *
55  * If you get a reference to a vport through a dp_port, it is protected
56  * by RCU and you need to hold rcu_read_lock instead when reading.
57  *
58  * If multiple locks are taken, the hierarchy is:
59  * 1. RTNL
60  * 2. DP
61  * 3. vport
62  */
63 static DEFINE_MUTEX(vport_mutex);
64
65 /**
66  *      vport_lock - acquire vport lock
67  *
68  * Acquire global vport lock.  See above comment about locking requirements
69  * and specific function definitions.  May sleep.
70  */
71 void vport_lock(void)
72 {
73         mutex_lock(&vport_mutex);
74 }
75
76 /**
77  *      vport_unlock - release vport lock
78  *
79  * Release lock acquired with vport_lock.
80  */
81 void vport_unlock(void)
82 {
83         mutex_unlock(&vport_mutex);
84 }
85
86 #define ASSERT_VPORT() do { \
87         if (unlikely(!mutex_is_locked(&vport_mutex))) { \
88                 printk(KERN_ERR "openvswitch: vport lock not held at %s (%d)\n", \
89                         __FILE__, __LINE__); \
90                 dump_stack(); \
91         } \
92 } while(0)
93
94 /**
95  *      vport_init - initialize vport subsystem
96  *
97  * Called at module load time to initialize the vport subsystem and any
98  * compiled in vport types.
99  */
100 int vport_init(void)
101 {
102         int err;
103         int i;
104
105         dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
106                             GFP_KERNEL);
107         if (!dev_table) {
108                 err = -ENOMEM;
109                 goto error;
110         }
111
112         vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
113                                  sizeof(struct vport_ops *), GFP_KERNEL);
114         if (!vport_ops_list) {
115                 err = -ENOMEM;
116                 goto error_dev_table;
117         }
118
119         for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
120                 struct vport_ops *new_ops = base_vport_ops_list[i];
121
122                 if (new_ops->init)
123                         err = new_ops->init();
124                 else
125                         err = 0;
126
127                 if (!err)
128                         vport_ops_list[n_vport_types++] = new_ops;
129                 else if (new_ops->flags & VPORT_F_REQUIRED) {
130                         vport_exit();
131                         goto error;
132                 }
133         }
134
135         return 0;
136
137 error_dev_table:
138         kfree(dev_table);
139 error:
140         return err;
141 }
142
143 static void vport_del_all(void)
144 {
145         int i;
146
147         rtnl_lock();
148         vport_lock();
149
150         for (i = 0; i < VPORT_HASH_BUCKETS; i++) {
151                 struct hlist_head *bucket = &dev_table[i];
152                 struct vport *vport;
153                 struct hlist_node *node, *next;
154
155                 hlist_for_each_entry_safe(vport, node, next, bucket, hash_node)
156                         vport_del(vport);
157         }
158
159         vport_unlock();
160         rtnl_unlock();
161 }
162
163 /**
164  *      vport_exit - shutdown vport subsystem
165  *
166  * Called at module exit time to shutdown the vport subsystem and any
167  * initialized vport types.
168  */
169 void vport_exit(void)
170 {
171         int i;
172
173         vport_del_all();
174
175         for (i = 0; i < n_vport_types; i++) {
176                 if (vport_ops_list[i]->exit)
177                         vport_ops_list[i]->exit();
178         }
179
180         kfree(vport_ops_list);
181         kfree(dev_table);
182 }
183
184 static int do_vport_add(struct odp_vport_add *vport_config)
185 {
186         struct vport *vport;
187         int err = 0;
188
189         vport_config->port_type[VPORT_TYPE_SIZE - 1] = '\0';
190         vport_config->devname[IFNAMSIZ - 1] = '\0';
191
192         rtnl_lock();
193
194         vport = vport_locate(vport_config->devname);
195         if (vport) {
196                 err = -EBUSY;
197                 goto out;
198         }
199
200         vport_lock();
201         vport = vport_add(vport_config->devname, vport_config->port_type,
202                           vport_config->config);
203         vport_unlock();
204
205         if (IS_ERR(vport))
206                 err = PTR_ERR(vport);
207
208 out:
209         rtnl_unlock();
210         return err;
211 }
212
213 /**
214  *      vport_user_add - add vport device (for userspace callers)
215  *
216  * @uvport_config: New port configuration.
217  *
218  * Creates a new vport with the specified configuration (which is dependent
219  * on device type).  This function is for userspace callers and assumes no
220  * locks are held.
221  */
222 int vport_user_add(const struct odp_vport_add __user *uvport_config)
223 {
224         struct odp_vport_add vport_config;
225
226         if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_add)))
227                 return -EFAULT;
228
229         return do_vport_add(&vport_config);
230 }
231
232 #ifdef CONFIG_COMPAT
233 int compat_vport_user_add(struct compat_odp_vport_add *ucompat)
234 {
235         struct compat_odp_vport_add compat;
236         struct odp_vport_add vport_config;
237
238         if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_add)))
239                 return -EFAULT;
240
241         memcpy(vport_config.port_type, compat.port_type, VPORT_TYPE_SIZE);
242         memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
243         vport_config.config = compat_ptr(compat.config);
244
245         return do_vport_add(&vport_config);
246 }
247 #endif
248
249 static int do_vport_mod(struct odp_vport_mod *vport_config)
250 {
251         struct vport *vport;
252         int err;
253
254         vport_config->devname[IFNAMSIZ - 1] = '\0';
255
256         rtnl_lock();
257
258         vport = vport_locate(vport_config->devname);
259         if (!vport) {
260                 err = -ENODEV;
261                 goto out;
262         }
263
264         vport_lock();
265         err = vport_mod(vport, vport_config->config);
266         vport_unlock();
267
268 out:
269         rtnl_unlock();
270         return err;
271 }
272
273 /**
274  *      vport_user_mod - modify existing vport device (for userspace callers)
275  *
276  * @uvport_config: New configuration for vport
277  *
278  * Modifies an existing device with the specified configuration (which is
279  * dependent on device type).  This function is for userspace callers and
280  * assumes no locks are held.
281  */
282 int vport_user_mod(const struct odp_vport_mod __user *uvport_config)
283 {
284         struct odp_vport_mod vport_config;
285
286         if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_mod)))
287                 return -EFAULT;
288
289         return do_vport_mod(&vport_config);
290 }
291
292 #ifdef CONFIG_COMPAT
293 int compat_vport_user_mod(struct compat_odp_vport_mod *ucompat)
294 {
295         struct compat_odp_vport_mod compat;
296         struct odp_vport_mod vport_config;
297
298         if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_mod)))
299                 return -EFAULT;
300
301         memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
302         vport_config.config = compat_ptr(compat.config);
303
304         return do_vport_mod(&vport_config);
305 }
306 #endif
307
308 /**
309  *      vport_user_del - delete existing vport device (for userspace callers)
310  *
311  * @udevname: Name of device to delete
312  *
313  * Deletes the specified device.  Detaches the device from a datapath first
314  * if it is attached.  Deleting the device will fail if it does not exist or it
315  * is the datapath local port.  It is also possible to fail for less obvious
316  * reasons, such as lack of memory.  This function is for userspace callers and
317  * assumes no locks are held.
318  */
319 int vport_user_del(const char __user *udevname)
320 {
321         char devname[IFNAMSIZ];
322         struct vport *vport;
323         struct dp_port *dp_port;
324         int err = 0;
325         int retval;
326
327         retval = strncpy_from_user(devname, udevname, IFNAMSIZ);
328         if (retval < 0)
329                 return -EFAULT;
330         else if (retval >= IFNAMSIZ)
331                 return -ENAMETOOLONG;
332
333         rtnl_lock();
334
335         vport = vport_locate(devname);
336         if (!vport) {
337                 err = -ENODEV;
338                 goto out;
339         }
340
341         dp_port = vport_get_dp_port(vport);
342         if (dp_port) {
343                 struct datapath *dp = dp_port->dp;
344
345                 mutex_lock(&dp->mutex);
346
347                 if (!strcmp(dp_name(dp), devname)) {
348                         err = -EINVAL;
349                         goto dp_port_out;
350                 }
351
352                 err = dp_detach_port(dp_port, 0);
353
354 dp_port_out:
355                 mutex_unlock(&dp->mutex);
356
357                 if (err)
358                         goto out;
359         }
360
361         vport_lock();
362         err = vport_del(vport);
363         vport_unlock();
364
365 out:
366         rtnl_unlock();
367         return err;
368 }
369
370 /**
371  *      vport_user_stats_get - retrieve device stats (for userspace callers)
372  *
373  * @ustats_req: Stats request parameters.
374  *
375  * Retrieves transmit, receive, and error stats for the given device.  This
376  * function is for userspace callers and assumes no locks are held.
377  */
378 int vport_user_stats_get(struct odp_vport_stats_req __user *ustats_req)
379 {
380         struct odp_vport_stats_req stats_req;
381         struct vport *vport;
382         int err;
383
384         if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
385                 return -EFAULT;
386
387         stats_req.devname[IFNAMSIZ - 1] = '\0';
388
389         vport_lock();
390
391         vport = vport_locate(stats_req.devname);
392         if (!vport) {
393                 err = -ENODEV;
394                 goto out;
395         }
396
397         err = vport_get_stats(vport, &stats_req.stats);
398
399 out:
400         vport_unlock();
401
402         if (!err)
403                 if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
404                         err = -EFAULT;
405
406         return err;
407 }
408
409 /**
410  *      vport_user_stats_set - sets offset device stats (for userspace callers)
411  *
412  * @ustats_req: Stats set parameters.
413  *
414  * Provides a set of transmit, receive, and error stats to be added as an
415  * offset to the collect data when stats are retreived.  Some devices may not
416  * support setting the stats, in which case the result will always be
417  * -EOPNOTSUPP.  This function is for userspace callers and assumes no locks
418  * are held.
419  */
420 int vport_user_stats_set(struct odp_vport_stats_req __user *ustats_req)
421 {
422         struct odp_vport_stats_req stats_req;
423         struct vport *vport;
424         int err;
425
426         if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
427                 return -EFAULT;
428
429         stats_req.devname[IFNAMSIZ - 1] = '\0';
430
431         rtnl_lock();
432         vport_lock();
433
434         vport = vport_locate(stats_req.devname);
435         if (!vport) {
436                 err = -ENODEV;
437                 goto out;
438         }
439
440         err = vport_set_stats(vport, &stats_req.stats);
441
442 out:
443         vport_unlock();
444         rtnl_unlock();
445         return err;
446 }
447
448
449 /**
450  *      vport_user_ether_get - retrieve device Ethernet address (for userspace callers)
451  *
452  * @uvport_ether: Ethernet address request parameters.
453  *
454  * Retrieves the Ethernet address of the given device.  This function is for
455  * userspace callers and assumes no locks are held.
456  */
457 int vport_user_ether_get(struct odp_vport_ether __user *uvport_ether)
458 {
459         struct odp_vport_ether vport_ether;
460         struct vport *vport;
461         int err = 0;
462
463         if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
464                 return -EFAULT;
465
466         vport_ether.devname[IFNAMSIZ - 1] = '\0';
467
468         vport_lock();
469
470         vport = vport_locate(vport_ether.devname);
471         if (!vport) {
472                 err = -ENODEV;
473                 goto out;
474         }
475
476         rcu_read_lock();
477         memcpy(vport_ether.ether_addr, vport_get_addr(vport), ETH_ALEN);
478         rcu_read_unlock();
479
480 out:
481         vport_unlock();
482
483         if (!err)
484                 if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct odp_vport_ether)))
485                         err = -EFAULT;
486
487         return err;
488 }
489
490 /**
491  *      vport_user_ether_set - set device Ethernet address (for userspace callers)
492  *
493  * @uvport_ether: Ethernet address request parameters.
494  *
495  * Sets the Ethernet address of the given device.  Some devices may not support
496  * setting the Ethernet address, in which case the result will always be
497  * -EOPNOTSUPP.  This function is for userspace callers and assumes no locks
498  * are held.
499  */
500 int vport_user_ether_set(struct odp_vport_ether __user *uvport_ether)
501 {
502         struct odp_vport_ether vport_ether;
503         struct vport *vport;
504         int err;
505
506         if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
507                 return -EFAULT;
508
509         vport_ether.devname[IFNAMSIZ - 1] = '\0';
510
511         rtnl_lock();
512         vport_lock();
513
514         vport = vport_locate(vport_ether.devname);
515         if (!vport) {
516                 err = -ENODEV;
517                 goto out;
518         }
519
520         err = vport_set_addr(vport, vport_ether.ether_addr);
521
522 out:
523         vport_unlock();
524         rtnl_unlock();
525         return err;
526 }
527
528 /**
529  *      vport_user_mtu_get - retrieve device MTU (for userspace callers)
530  *
531  * @uvport_mtu: MTU request parameters.
532  *
533  * Retrieves the MTU of the given device.  This function is for userspace
534  * callers and assumes no locks are held.
535  */
536 int vport_user_mtu_get(struct odp_vport_mtu __user *uvport_mtu)
537 {
538         struct odp_vport_mtu vport_mtu;
539         struct vport *vport;
540         int err = 0;
541
542         if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
543                 return -EFAULT;
544
545         vport_mtu.devname[IFNAMSIZ - 1] = '\0';
546
547         vport_lock();
548
549         vport = vport_locate(vport_mtu.devname);
550         if (!vport) {
551                 err = -ENODEV;
552                 goto out;
553         }
554
555         vport_mtu.mtu = vport_get_mtu(vport);
556
557 out:
558         vport_unlock();
559
560         if (!err)
561                 if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct odp_vport_mtu)))
562                         err = -EFAULT;
563
564         return err;
565 }
566
567 /**
568  *      vport_user_mtu_set - set device MTU (for userspace callers)
569  *
570  * @uvport_mtu: MTU request parameters.
571  *
572  * Sets the MTU of the given device.  Some devices may not support setting the
573  * MTU, in which case the result will always be -EOPNOTSUPP.  This function is
574  * for userspace callers and assumes no locks are held.
575  */
576 int vport_user_mtu_set(struct odp_vport_mtu __user *uvport_mtu)
577 {
578         struct odp_vport_mtu vport_mtu;
579         struct vport *vport;
580         int err;
581
582         if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
583                 return -EFAULT;
584
585         vport_mtu.devname[IFNAMSIZ - 1] = '\0';
586
587         rtnl_lock();
588         vport_lock();
589
590         vport = vport_locate(vport_mtu.devname);
591         if (!vport) {
592                 err = -ENODEV;
593                 goto out;
594         }
595
596         err = vport_set_mtu(vport, vport_mtu.mtu);
597
598 out:
599         vport_unlock();
600         rtnl_unlock();
601         return err;
602 }
603
604 static struct hlist_head *hash_bucket(const char *name)
605 {
606         unsigned int hash = full_name_hash(name, strlen(name));
607         return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
608 }
609
610 /**
611  *      vport_locate - find a port that has already been created
612  *
613  * @name: name of port to find
614  *
615  * Either RTNL or vport lock must be acquired before calling this function
616  * and held while using the found port.  See the locking comments at the
617  * top of the file.
618  */
619 struct vport *vport_locate(const char *name)
620 {
621         struct hlist_head *bucket = hash_bucket(name);
622         struct vport *vport;
623         struct hlist_node *node;
624
625         if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
626                 printk(KERN_ERR "openvswitch: neither RTNL nor vport lock held in vport_locate\n");
627                 dump_stack();
628         }
629
630         rcu_read_lock();
631
632         hlist_for_each_entry(vport, node, bucket, hash_node)
633                 if (!strcmp(name, vport_get_name(vport)))
634                         goto out;
635
636         vport = NULL;
637
638 out:
639         rcu_read_unlock();
640         return vport;
641 }
642
643 static void register_vport(struct vport *vport)
644 {
645         hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
646 }
647
648 static void unregister_vport(struct vport *vport)
649 {
650         hlist_del(&vport->hash_node);
651 }
652
653 /**
654  *      vport_alloc - allocate and initialize new vport
655  *
656  * @priv_size: Size of private data area to allocate.
657  * @ops: vport device ops
658  *
659  * Allocate and initialize a new vport defined by @ops.  The vport will contain
660  * a private data area of size @priv_size that can be accessed using
661  * vport_priv().  vports that are no longer needed should be released with
662  * vport_free().
663  */
664 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops)
665 {
666         struct vport *vport;
667         size_t alloc_size;
668
669         alloc_size = sizeof(struct vport);
670         if (priv_size) {
671                 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
672                 alloc_size += priv_size;
673         }
674
675         vport = kzalloc(alloc_size, GFP_KERNEL);
676         if (!vport)
677                 return ERR_PTR(-ENOMEM);
678
679         vport->ops = ops;
680
681         if (vport->ops->flags & VPORT_F_GEN_STATS) {
682                 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
683                 if (!vport->percpu_stats)
684                         return ERR_PTR(-ENOMEM);
685
686                 spin_lock_init(&vport->stats_lock);
687         }
688
689         return vport;
690 }
691
692 /**
693  *      vport_free - uninitialize and free vport
694  *
695  * @vport: vport to free
696  *
697  * Frees a vport allocated with vport_alloc() when it is no longer needed.
698  */
699 void vport_free(struct vport *vport)
700 {
701         if (vport->ops->flags & VPORT_F_GEN_STATS)
702                 free_percpu(vport->percpu_stats);
703
704         kfree(vport);
705 }
706
707 /**
708  *      vport_add - add vport device (for kernel callers)
709  *
710  * @name: Name of new device.
711  * @type: Type of new device (to be matched against types in registered vport
712  * ops).
713  * @config: Device type specific configuration.  Userspace pointer.
714  *
715  * Creates a new vport with the specified configuration (which is dependent
716  * on device type).  Both RTNL and vport locks must be held.
717  */
718 struct vport *vport_add(const char *name, const char *type, const void __user *config)
719 {
720         struct vport *vport;
721         int err = 0;
722         int i;
723
724         ASSERT_RTNL();
725         ASSERT_VPORT();
726
727         for (i = 0; i < n_vport_types; i++) {
728                 if (!strcmp(vport_ops_list[i]->type, type)) {
729                         vport = vport_ops_list[i]->create(name, config);
730                         if (IS_ERR(vport)) {
731                                 err = PTR_ERR(vport);
732                                 goto out;
733                         }
734
735                         register_vport(vport);
736                         return vport;
737                 }
738         }
739
740         err = -EAFNOSUPPORT;
741
742 out:
743         return ERR_PTR(err);
744 }
745
746 /**
747  *      vport_mod - modify existing vport device (for kernel callers)
748  *
749  * @vport: vport to modify.
750  * @config: Device type specific configuration.  Userspace pointer.
751  *
752  * Modifies an existing device with the specified configuration (which is
753  * dependent on device type).  Both RTNL and vport locks must be held.
754  */
755 int vport_mod(struct vport *vport, const void __user *config)
756 {
757         ASSERT_RTNL();
758         ASSERT_VPORT();
759
760         if (vport->ops->modify)
761                 return vport->ops->modify(vport, config);
762         else
763                 return -EOPNOTSUPP;
764 }
765
766 /**
767  *      vport_del - delete existing vport device (for kernel callers)
768  *
769  * @vport: vport to delete.
770  *
771  * Deletes the specified device.  The device must not be currently attached to
772  * a datapath.  It is possible to fail for reasons such as lack of memory.
773  * Both RTNL and vport locks must be held.
774  */
775 int vport_del(struct vport *vport)
776 {
777         ASSERT_RTNL();
778         ASSERT_VPORT();
779         BUG_ON(vport_get_dp_port(vport));
780
781         unregister_vport(vport);
782
783         return vport->ops->destroy(vport);
784 }
785
786 /**
787  *      vport_attach - attach a vport to a datapath
788  *
789  * @vport: vport to attach.
790  * @dp_port: Datapath port to attach the vport to.
791  *
792  * Attaches a vport to a specific datapath so that packets may be exchanged.
793  * Both ports must be currently unattached.  @dp_port must be successfully
794  * attached to a vport before it is connected to a datapath and must not be
795  * modified while connected.  RTNL lock and the appropriate DP mutex must be held.
796  */
797 int vport_attach(struct vport *vport, struct dp_port *dp_port)
798 {
799         ASSERT_RTNL();
800
801         if (dp_port->vport)
802                 return -EBUSY;
803
804         if (vport_get_dp_port(vport))
805                 return -EBUSY;
806
807         if (vport->ops->attach) {
808                 int err;
809
810                 err = vport->ops->attach(vport);
811                 if (err)
812                         return err;
813         }
814
815         dp_port->vport = vport;
816         rcu_assign_pointer(vport->dp_port, dp_port);
817
818         return 0;
819 }
820
821 /**
822  *      vport_detach - detach a vport from a datapath
823  *
824  * @vport: vport to detach.
825  *
826  * Detaches a vport from a datapath.  May fail for a variety of reasons,
827  * including lack of memory.  RTNL lock and the appropriate DP mutex must be held.
828  */
829 int vport_detach(struct vport *vport)
830 {
831         struct dp_port *dp_port;
832
833         ASSERT_RTNL();
834
835         dp_port = vport_get_dp_port(vport);
836         if (!dp_port)
837                 return -EINVAL;
838
839         dp_port->vport = NULL;
840         rcu_assign_pointer(vport->dp_port, NULL);
841
842         if (vport->ops->detach)
843                 return vport->ops->detach(vport);
844         else
845                 return 0;
846 }
847
848 /**
849  *      vport_set_mtu - set device MTU (for kernel callers)
850  *
851  * @vport: vport on which to set MTU.
852  * @mtu: New MTU.
853  *
854  * Sets the MTU of the given device.  Some devices may not support setting the
855  * MTU, in which case the result will always be -EOPNOTSUPP.  RTNL lock must
856  * be held.
857  */
858 int vport_set_mtu(struct vport *vport, int mtu)
859 {
860         ASSERT_RTNL();
861
862         if (mtu < 68)
863                 return -EINVAL;
864
865         if (vport->ops->set_mtu) {
866                 int ret;
867
868                 ret = vport->ops->set_mtu(vport, mtu);
869
870                 if (!ret && !is_internal_vport(vport)) {
871                         struct dp_port *dp_port = vport_get_dp_port(vport);
872
873                         if (dp_port)
874                                 set_internal_devs_mtu(dp_port->dp);
875                 }
876
877                 return ret;
878         } else
879                 return -EOPNOTSUPP;
880 }
881
882 /**
883  *      vport_set_addr - set device Ethernet address (for kernel callers)
884  *
885  * @vport: vport on which to set Ethernet address.
886  * @addr: New address.
887  *
888  * Sets the Ethernet address of the given device.  Some devices may not support
889  * setting the Ethernet address, in which case the result will always be
890  * -EOPNOTSUPP.  RTNL lock must be held.
891  */
892 int vport_set_addr(struct vport *vport, const unsigned char *addr)
893 {
894         ASSERT_RTNL();
895
896         if (!is_valid_ether_addr(addr))
897                 return -EADDRNOTAVAIL;
898
899         if (vport->ops->set_addr)
900                 return vport->ops->set_addr(vport, addr);
901         else
902                 return -EOPNOTSUPP;
903 }
904
905 /**
906  *      vport_set_stats - sets offset device stats (for kernel callers)
907  *
908  * @vport: vport on which to set stats
909  * @stats: stats to set
910  *
911  * Provides a set of transmit, receive, and error stats to be added as an
912  * offset to the collect data when stats are retreived.  Some devices may not
913  * support setting the stats, in which case the result will always be
914  * -EOPNOTSUPP.  RTNL lock must be held.
915  */
916 int vport_set_stats(struct vport *vport, struct odp_vport_stats *stats)
917 {
918         ASSERT_RTNL();
919
920         if (vport->ops->flags & VPORT_F_GEN_STATS) {
921                 spin_lock_bh(&vport->stats_lock);
922                 memcpy(&vport->offset_stats, stats, sizeof(struct odp_vport_stats));
923                 spin_unlock_bh(&vport->stats_lock);
924
925                 return 0;
926         } else if (vport->ops->set_stats)
927                 return vport->ops->set_stats(vport, stats);
928         else
929                 return -EOPNOTSUPP;
930 }
931
932 /**
933  *      vport_get_name - retrieve device name
934  *
935  * @vport: vport from which to retrieve the name.
936  *
937  * Retrieves the name of the given device.  Either RTNL lock or rcu_read_lock
938  * must be held for the entire duration that the name is in use.
939  */
940 const char *vport_get_name(const struct vport *vport)
941 {
942         return vport->ops->get_name(vport);
943 }
944
945 /**
946  *      vport_get_type - retrieve device type
947  *
948  * @vport: vport from which to retrieve the type.
949  *
950  * Retrieves the type of the given device.  Either RTNL lock or rcu_read_lock
951  * must be held for the entire duration that the type is in use.
952  */
953 const char *vport_get_type(const struct vport *vport)
954 {
955         return vport->ops->type;
956 }
957
958 /**
959  *      vport_get_addr - retrieve device Ethernet address (for kernel callers)
960  *
961  * @vport: vport from which to retrieve the Ethernet address.
962  *
963  * Retrieves the Ethernet address of the given device.  Either RTNL lock or
964  * rcu_read_lock must be held for the entire duration that the Ethernet address
965  * is in use.
966  */
967 const unsigned char *vport_get_addr(const struct vport *vport)
968 {
969         return vport->ops->get_addr(vport);
970 }
971
972 /**
973  *      vport_get_dp_port - retrieve attached datapath port
974  *
975  * @vport: vport from which to retrieve the datapath port.
976  *
977  * Retrieves the attached datapath port or null if not attached.  Either RTNL
978  * lock or rcu_read_lock must be held for the entire duration that the datapath
979  * port is being accessed.
980  */
981 struct dp_port *vport_get_dp_port(const struct vport *vport)
982 {
983         return rcu_dereference(vport->dp_port);
984 }
985
986 /**
987  *      vport_get_kobj - retrieve associated kobj
988  *
989  * @vport: vport from which to retrieve the associated kobj
990  *
991  * Retrieves the associated kobj or null if no kobj.  The returned kobj is
992  * valid for as long as the vport exists.
993  */
994 struct kobject *vport_get_kobj(const struct vport *vport)
995 {
996         if (vport->ops->get_kobj)
997                 return vport->ops->get_kobj(vport);
998         else
999                 return NULL;
1000 }
1001
1002 /**
1003  *      vport_get_stats - retrieve device stats (for kernel callers)
1004  *
1005  * @vport: vport from which to retrieve the stats
1006  * @stats: location to store stats
1007  *
1008  * Retrieves transmit, receive, and error stats for the given device.
1009  */
1010 int vport_get_stats(struct vport *vport, struct odp_vport_stats *stats)
1011 {
1012         struct odp_vport_stats dev_stats;
1013         struct odp_vport_stats *dev_statsp = NULL;
1014         int err;
1015
1016         if (vport->ops->get_stats) {
1017                 if (vport->ops->flags & VPORT_F_GEN_STATS)
1018                         dev_statsp = &dev_stats;
1019                 else
1020                         dev_statsp = stats;
1021
1022                 rcu_read_lock();
1023                 err = vport->ops->get_stats(vport, dev_statsp);
1024                 rcu_read_unlock();
1025
1026                 if (err)
1027                         goto out;
1028         }
1029
1030         if (vport->ops->flags & VPORT_F_GEN_STATS) {
1031                 int i;
1032
1033                 /* We potentially have 3 sources of stats that need to be
1034                  * combined: those we have collected (split into err_stats and
1035                  * percpu_stats), offset_stats from set_stats(), and device
1036                  * error stats from get_stats() (for errors that happen
1037                  * downstream and therefore aren't reported through our
1038                  * vport_record_error() function). */
1039
1040                 spin_lock_bh(&vport->stats_lock);
1041
1042                 memcpy(stats, &vport->offset_stats, sizeof(struct odp_vport_stats));
1043
1044                 stats->rx_errors        += vport->err_stats.rx_errors
1045                                                 + vport->err_stats.rx_frame_err
1046                                                 + vport->err_stats.rx_over_err
1047                                                 + vport->err_stats.rx_crc_err;
1048                 stats->tx_errors        += vport->err_stats.tx_errors;
1049                 stats->tx_dropped       += vport->err_stats.tx_dropped;
1050                 stats->rx_dropped       += vport->err_stats.rx_dropped;
1051                 stats->rx_over_err      += vport->err_stats.rx_over_err;
1052                 stats->rx_crc_err       += vport->err_stats.rx_crc_err;
1053                 stats->rx_frame_err     += vport->err_stats.rx_frame_err;
1054                 stats->collisions       += vport->err_stats.collisions;
1055
1056                 spin_unlock_bh(&vport->stats_lock);
1057
1058                 if (dev_statsp) {
1059                         stats->rx_errors        += dev_statsp->rx_errors;
1060                         stats->tx_errors        += dev_statsp->tx_errors;
1061                         stats->rx_dropped       += dev_statsp->rx_dropped;
1062                         stats->tx_dropped       += dev_statsp->tx_dropped;
1063                         stats->rx_over_err      += dev_statsp->rx_over_err;
1064                         stats->rx_crc_err       += dev_statsp->rx_crc_err;
1065                         stats->rx_frame_err     += dev_statsp->rx_frame_err;
1066                         stats->collisions       += dev_statsp->collisions;
1067                 }
1068
1069                 for_each_possible_cpu(i) {
1070                         const struct vport_percpu_stats *percpu_stats;
1071
1072                         percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
1073                         stats->rx_bytes         += percpu_stats->rx_bytes;
1074                         stats->rx_packets       += percpu_stats->rx_packets;
1075                         stats->tx_bytes         += percpu_stats->tx_bytes;
1076                         stats->tx_packets       += percpu_stats->tx_packets;
1077                 }
1078
1079                 err = 0;
1080         } else
1081                 err = -EOPNOTSUPP;
1082
1083 out:
1084         return err;
1085 }
1086
1087 /**
1088  *      vport_get_flags - retrieve device flags
1089  *
1090  * @vport: vport from which to retrieve the flags
1091  *
1092  * Retrieves the flags of the given device.  Either RTNL lock or rcu_read_lock
1093  * must be held.
1094  */
1095 unsigned vport_get_flags(const struct vport *vport)
1096 {
1097         return vport->ops->get_dev_flags(vport);
1098 }
1099
1100 /**
1101  *      vport_get_flags - check whether device is running
1102  *
1103  * @vport: vport on which to check status.
1104  *
1105  * Checks whether the given device is running.  Either RTNL lock or
1106  * rcu_read_lock must be held.
1107  */
1108 int vport_is_running(const struct vport *vport)
1109 {
1110         return vport->ops->is_running(vport);
1111 }
1112
1113 /**
1114  *      vport_get_flags - retrieve device operating state
1115  *
1116  * @vport: vport from which to check status
1117  *
1118  * Retrieves the RFC2863 operstate of the given device.  Either RTNL lock or
1119  * rcu_read_lock must be held.
1120  */
1121 unsigned char vport_get_operstate(const struct vport *vport)
1122 {
1123         return vport->ops->get_operstate(vport);
1124 }
1125
1126 /**
1127  *      vport_get_ifindex - retrieve device system interface index
1128  *
1129  * @vport: vport from which to retrieve index
1130  *
1131  * Retrieves the system interface index of the given device.  Not all devices
1132  * will have system indexes, in which case the index of the datapath local
1133  * port is returned.  Returns a negative index on error.  Either RTNL lock or
1134  * rcu_read_lock must be held.
1135  */
1136 int vport_get_ifindex(const struct vport *vport)
1137 {
1138         const struct dp_port *dp_port;
1139
1140         if (vport->ops->get_ifindex)
1141                 return vport->ops->get_ifindex(vport);
1142
1143         /* If we don't actually have an ifindex, use the local port's.
1144          * Userspace doesn't check it anyways. */
1145         dp_port = vport_get_dp_port(vport);
1146         if (!dp_port)
1147                 return -EAGAIN;
1148
1149         return vport_get_ifindex(dp_port->dp->ports[ODPP_LOCAL]->vport);
1150 }
1151
1152 /**
1153  *      vport_get_iflink - retrieve device system link index
1154  *
1155  * @vport: vport from which to retrieve index
1156  *
1157  * Retrieves the system link index of the given device.  The link is the index
1158  * of the interface on which the packet will actually be sent.  In most cases
1159  * this is the same as the ifindex but may be different for tunnel devices.
1160  * Returns a negative index on error.  Either RTNL lock or rcu_read_lock must
1161  * be held.
1162  */
1163 int vport_get_iflink(const struct vport *vport)
1164 {
1165         if (vport->ops->get_iflink)
1166                 return vport->ops->get_iflink(vport);
1167
1168         /* If we don't have an iflink, use the ifindex.  In most cases they
1169          * are the same. */
1170         return vport_get_ifindex(vport);
1171 }
1172
1173 /**
1174  *      vport_get_mtu - retrieve device MTU (for kernel callers)
1175  *
1176  * @vport: vport from which to retrieve MTU
1177  *
1178  * Retrieves the MTU of the given device.  Either RTNL lock or rcu_read_lock
1179  * must be held.
1180  */
1181 int vport_get_mtu(const struct vport *vport)
1182 {
1183         return vport->ops->get_mtu(vport);
1184 }
1185
1186 /**
1187  *      vport_receive - pass up received packet to the datapath for processing
1188  *
1189  * @vport: vport that received the packet
1190  * @skb: skb that was received
1191  *
1192  * Must be called with rcu_read_lock.  The packet cannot be shared and
1193  * skb->data should point to the Ethernet header.  The caller must have already
1194  * called compute_ip_summed() to initialize the checksumming fields.
1195  */
1196 void vport_receive(struct vport *vport, struct sk_buff *skb)
1197 {
1198         struct dp_port *dp_port = vport_get_dp_port(vport);
1199
1200         if (!dp_port) {
1201                 vport_record_error(vport, VPORT_E_RX_DROPPED);
1202                 kfree_skb(skb);
1203
1204                 return;
1205         }
1206
1207         if (vport->ops->flags & VPORT_F_GEN_STATS) {
1208                 struct vport_percpu_stats *stats;
1209
1210                 local_bh_disable();
1211
1212                 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1213                 stats->rx_packets++;
1214                 stats->rx_bytes += skb->len;
1215
1216                 local_bh_enable();
1217         }
1218
1219         if (!(vport->ops->flags & VPORT_F_TUN_ID))
1220                 OVS_CB(skb)->tun_id = 0;
1221
1222         dp_process_received_packet(dp_port, skb);
1223 }
1224
1225 static inline unsigned packet_length(const struct sk_buff *skb)
1226 {
1227         unsigned length = skb->len - ETH_HLEN;
1228
1229         if (skb->protocol == htons(ETH_P_8021Q))
1230                 length -= VLAN_HLEN;
1231
1232         return length;
1233 }
1234
1235 /**
1236  *      vport_send - send a packet on a device
1237  *
1238  * @vport: vport on which to send the packet
1239  * @skb: skb to send
1240  *
1241  * Sends the given packet and returns the length of data sent.  Either RTNL
1242  * lock or rcu_read_lock must be held.
1243  */
1244 int vport_send(struct vport *vport, struct sk_buff *skb)
1245 {
1246         int *loop_count;
1247         int mtu;
1248         int sent;
1249
1250         loop_count = &get_cpu_var(vport_loop_counter).count[!!in_interrupt()];
1251         (*loop_count)++;
1252
1253         if (unlikely(*loop_count > VPORT_MAX_LOOPS)) {
1254                 if (net_ratelimit())
1255                         printk(KERN_WARNING "%s: dropping packet that has looped more than %d times\n",
1256                                dp_name(vport_get_dp_port(vport)->dp), VPORT_MAX_LOOPS);
1257                 goto error;
1258         }
1259
1260         mtu = vport_get_mtu(vport);
1261         if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
1262                 if (net_ratelimit())
1263                         printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
1264                                dp_name(vport_get_dp_port(vport)->dp), packet_length(skb), mtu);
1265                 goto error;
1266         }
1267
1268         sent = vport->ops->send(vport, skb);
1269
1270         if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
1271                 struct vport_percpu_stats *stats;
1272
1273                 local_bh_disable();
1274
1275                 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1276                 stats->tx_packets++;
1277                 stats->tx_bytes += sent;
1278
1279                 local_bh_enable();
1280         }
1281
1282         goto out;
1283
1284 error:
1285         sent = 0;
1286         kfree_skb(skb);
1287         vport_record_error(vport, VPORT_E_TX_DROPPED);
1288 out:
1289         (*loop_count)--;
1290         put_cpu_var(vport_loop_counter);
1291
1292         return sent;
1293 }
1294
1295 /**
1296  *      vport_record_error - indicate device error to generic stats layer
1297  *
1298  * @vport: vport that encountered the error
1299  * @err_type: one of enum vport_err_type types to indicate the error type
1300  *
1301  * If using the vport generic stats layer indicate that an error of the given
1302  * type has occured.
1303  */
1304 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
1305 {
1306         if (vport->ops->flags & VPORT_F_GEN_STATS) {
1307
1308                 spin_lock_bh(&vport->stats_lock);
1309
1310                 switch (err_type) {
1311                 case VPORT_E_RX_DROPPED:
1312                         vport->err_stats.rx_dropped++;
1313                         break;
1314
1315                 case VPORT_E_RX_ERROR:
1316                         vport->err_stats.rx_errors++;
1317                         break;
1318
1319                 case VPORT_E_RX_FRAME:
1320                         vport->err_stats.rx_frame_err++;
1321                         break;
1322
1323                 case VPORT_E_RX_OVER:
1324                         vport->err_stats.rx_over_err++;
1325                         break;
1326
1327                 case VPORT_E_RX_CRC:
1328                         vport->err_stats.rx_crc_err++;
1329                         break;
1330
1331                 case VPORT_E_TX_DROPPED:
1332                         vport->err_stats.tx_dropped++;
1333                         break;
1334
1335                 case VPORT_E_TX_ERROR:
1336                         vport->err_stats.tx_errors++;
1337                         break;
1338
1339                 case VPORT_E_COLLISION:
1340                         vport->err_stats.collisions++;
1341                         break;
1342                 };
1343
1344                 spin_unlock_bh(&vport->stats_lock);
1345         }
1346 }