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