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