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