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