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