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