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