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