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