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