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