datapath: Disable LRO from userspace instead of the kernel.
[sliver-openvswitch.git] / datapath / vport.c
1 /*
2  * Copyright (c) 2010, 2011 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/rcupdate.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/compat.h>
20 #include <linux/version.h>
21
22 #include "vport.h"
23 #include "vport-internal_dev.h"
24
25 /* List of statically compiled vport implementations.  Don't forget to also
26  * add yours to the list at the bottom of vport.h. */
27 static const struct vport_ops *base_vport_ops_list[] = {
28         &netdev_vport_ops,
29         &internal_vport_ops,
30         &patch_vport_ops,
31         &gre_vport_ops,
32 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
33         &capwap_vport_ops,
34 #endif
35 };
36
37 static const struct vport_ops **vport_ops_list;
38 static int n_vport_types;
39
40 /* Protected by RCU read lock for reading, RTNL lock for writing. */
41 static struct hlist_head *dev_table;
42 #define VPORT_HASH_BUCKETS 1024
43
44 /**
45  *      vport_init - initialize vport subsystem
46  *
47  * Called at module load time to initialize the vport subsystem and any
48  * compiled in vport types.
49  */
50 int vport_init(void)
51 {
52         int err;
53         int i;
54
55         dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
56                             GFP_KERNEL);
57         if (!dev_table) {
58                 err = -ENOMEM;
59                 goto error;
60         }
61
62         vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
63                                  sizeof(struct vport_ops *), GFP_KERNEL);
64         if (!vport_ops_list) {
65                 err = -ENOMEM;
66                 goto error_dev_table;
67         }
68
69         for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
70                 const struct vport_ops *new_ops = base_vport_ops_list[i];
71
72                 if (new_ops->init)
73                         err = new_ops->init();
74                 else
75                         err = 0;
76
77                 if (!err)
78                         vport_ops_list[n_vport_types++] = new_ops;
79                 else if (new_ops->flags & VPORT_F_REQUIRED) {
80                         vport_exit();
81                         goto error;
82                 }
83         }
84
85         return 0;
86
87 error_dev_table:
88         kfree(dev_table);
89 error:
90         return err;
91 }
92
93 /**
94  *      vport_exit - shutdown vport subsystem
95  *
96  * Called at module exit time to shutdown the vport subsystem and any
97  * initialized vport types.
98  */
99 void vport_exit(void)
100 {
101         int i;
102
103         for (i = 0; i < n_vport_types; i++) {
104                 if (vport_ops_list[i]->exit)
105                         vport_ops_list[i]->exit();
106         }
107
108         kfree(vport_ops_list);
109         kfree(dev_table);
110 }
111
112 static struct hlist_head *hash_bucket(const char *name)
113 {
114         unsigned int hash = full_name_hash(name, strlen(name));
115         return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
116 }
117
118 /**
119  *      vport_locate - find a port that has already been created
120  *
121  * @name: name of port to find
122  *
123  * Must be called with RTNL or RCU read lock.
124  */
125 struct vport *vport_locate(const char *name)
126 {
127         struct hlist_head *bucket = hash_bucket(name);
128         struct vport *vport;
129         struct hlist_node *node;
130
131         hlist_for_each_entry_rcu(vport, node, bucket, hash_node)
132                 if (!strcmp(name, vport_get_name(vport)))
133                         return vport;
134
135         return NULL;
136 }
137
138 static void release_vport(struct kobject *kobj)
139 {
140         struct vport *p = container_of(kobj, struct vport, kobj);
141         kfree(p);
142 }
143
144 static struct kobj_type brport_ktype = {
145 #ifdef CONFIG_SYSFS
146         .sysfs_ops = &brport_sysfs_ops,
147 #endif
148         .release = release_vport
149 };
150
151 /**
152  *      vport_alloc - allocate and initialize new vport
153  *
154  * @priv_size: Size of private data area to allocate.
155  * @ops: vport device ops
156  *
157  * Allocate and initialize a new vport defined by @ops.  The vport will contain
158  * a private data area of size @priv_size that can be accessed using
159  * vport_priv().  vports that are no longer needed should be released with
160  * vport_free().
161  */
162 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops, const struct vport_parms *parms)
163 {
164         struct vport *vport;
165         size_t alloc_size;
166
167         alloc_size = sizeof(struct vport);
168         if (priv_size) {
169                 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
170                 alloc_size += priv_size;
171         }
172
173         vport = kzalloc(alloc_size, GFP_KERNEL);
174         if (!vport)
175                 return ERR_PTR(-ENOMEM);
176
177         vport->dp = parms->dp;
178         vport->port_no = parms->port_no;
179         atomic_set(&vport->sflow_pool, 0);
180         vport->ops = ops;
181
182         /* Initialize kobject for bridge.  This will be added as
183          * /sys/class/net/<devname>/brport later, if sysfs is enabled. */
184         vport->kobj.kset = NULL;
185         kobject_init(&vport->kobj, &brport_ktype);
186
187         if (vport->ops->flags & VPORT_F_GEN_STATS) {
188                 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
189                 if (!vport->percpu_stats)
190                         return ERR_PTR(-ENOMEM);
191
192                 spin_lock_init(&vport->stats_lock);
193         }
194
195         return vport;
196 }
197
198 /**
199  *      vport_free - uninitialize and free vport
200  *
201  * @vport: vport to free
202  *
203  * Frees a vport allocated with vport_alloc() when it is no longer needed.
204  *
205  * The caller must ensure that an RCU grace period has passed since the last
206  * time @vport was in a datapath.
207  */
208 void vport_free(struct vport *vport)
209 {
210         if (vport->ops->flags & VPORT_F_GEN_STATS)
211                 free_percpu(vport->percpu_stats);
212
213         kobject_put(&vport->kobj);
214 }
215
216 /**
217  *      vport_add - add vport device (for kernel callers)
218  *
219  * @parms: Information about new vport.
220  *
221  * Creates a new vport with the specified configuration (which is dependent on
222  * device type).  RTNL lock must be held.
223  */
224 struct vport *vport_add(const struct vport_parms *parms)
225 {
226         struct vport *vport;
227         int err = 0;
228         int i;
229
230         ASSERT_RTNL();
231
232         for (i = 0; i < n_vport_types; i++) {
233                 if (vport_ops_list[i]->type == parms->type) {
234                         vport = vport_ops_list[i]->create(parms);
235                         if (IS_ERR(vport)) {
236                                 err = PTR_ERR(vport);
237                                 goto out;
238                         }
239
240                         hlist_add_head_rcu(&vport->hash_node,
241                                            hash_bucket(vport_get_name(vport)));
242                         return vport;
243                 }
244         }
245
246         err = -EAFNOSUPPORT;
247
248 out:
249         return ERR_PTR(err);
250 }
251
252 /**
253  *      vport_set_options - modify existing vport device (for kernel callers)
254  *
255  * @vport: vport to modify.
256  * @port: New configuration.
257  *
258  * Modifies an existing device with the specified configuration (which is
259  * dependent on device type).  RTNL lock must be held.
260  */
261 int vport_set_options(struct vport *vport, struct nlattr *options)
262 {
263         ASSERT_RTNL();
264
265         if (!vport->ops->set_options)
266                 return -EOPNOTSUPP;
267         return vport->ops->set_options(vport, options);
268 }
269
270 /**
271  *      vport_del - delete existing vport device
272  *
273  * @vport: vport to delete.
274  *
275  * Detaches @vport from its datapath and destroys it.  It is possible to fail
276  * for reasons such as lack of memory.  RTNL lock must be held.
277  */
278 int vport_del(struct vport *vport)
279 {
280         ASSERT_RTNL();
281
282         hlist_del_rcu(&vport->hash_node);
283
284         return vport->ops->destroy(vport);
285 }
286
287 /**
288  *      vport_set_mtu - set device MTU (for kernel callers)
289  *
290  * @vport: vport on which to set MTU.
291  * @mtu: New MTU.
292  *
293  * Sets the MTU of the given device.  Some devices may not support setting the
294  * MTU, in which case the result will always be -EOPNOTSUPP.  RTNL lock must
295  * be held.
296  */
297 int vport_set_mtu(struct vport *vport, int mtu)
298 {
299         ASSERT_RTNL();
300
301         if (mtu < 68)
302                 return -EINVAL;
303
304         if (vport->ops->set_mtu) {
305                 int ret;
306
307                 ret = vport->ops->set_mtu(vport, mtu);
308
309                 if (!ret && !is_internal_vport(vport))
310                         set_internal_devs_mtu(vport->dp);
311
312                 return ret;
313         } else
314                 return -EOPNOTSUPP;
315 }
316
317 /**
318  *      vport_set_addr - set device Ethernet address (for kernel callers)
319  *
320  * @vport: vport on which to set Ethernet address.
321  * @addr: New address.
322  *
323  * Sets the Ethernet address of the given device.  Some devices may not support
324  * setting the Ethernet address, in which case the result will always be
325  * -EOPNOTSUPP.  RTNL lock must be held.
326  */
327 int vport_set_addr(struct vport *vport, const unsigned char *addr)
328 {
329         ASSERT_RTNL();
330
331         if (!is_valid_ether_addr(addr))
332                 return -EADDRNOTAVAIL;
333
334         if (vport->ops->set_addr)
335                 return vport->ops->set_addr(vport, addr);
336         else
337                 return -EOPNOTSUPP;
338 }
339
340 /**
341  *      vport_set_stats - sets offset device stats
342  *
343  * @vport: vport on which to set stats
344  * @stats: stats to set
345  *
346  * Provides a set of transmit, receive, and error stats to be added as an
347  * offset to the collect data when stats are retreived.  Some devices may not
348  * support setting the stats, in which case the result will always be
349  * -EOPNOTSUPP.
350  *
351  * Must be called with RTNL lock.
352  */
353 int vport_set_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
354 {
355         ASSERT_RTNL();
356
357         if (vport->ops->flags & VPORT_F_GEN_STATS) {
358                 spin_lock_bh(&vport->stats_lock);
359                 vport->offset_stats = *stats;
360                 spin_unlock_bh(&vport->stats_lock);
361
362                 return 0;
363         } else
364                 return -EOPNOTSUPP;
365 }
366
367 /**
368  *      vport_get_name - retrieve device name
369  *
370  * @vport: vport from which to retrieve the name.
371  *
372  * Retrieves the name of the given device.  Either RTNL lock or rcu_read_lock
373  * must be held for the entire duration that the name is in use.
374  */
375 const char *vport_get_name(const struct vport *vport)
376 {
377         return vport->ops->get_name(vport);
378 }
379
380 /**
381  *      vport_get_type - retrieve device type
382  *
383  * @vport: vport from which to retrieve the type.
384  *
385  * Retrieves the type of the given device.
386  */
387 enum ovs_vport_type vport_get_type(const struct vport *vport)
388 {
389         return vport->ops->type;
390 }
391
392 /**
393  *      vport_get_addr - retrieve device Ethernet address (for kernel callers)
394  *
395  * @vport: vport from which to retrieve the Ethernet address.
396  *
397  * Retrieves the Ethernet address of the given device.  Either RTNL lock or
398  * rcu_read_lock must be held for the entire duration that the Ethernet address
399  * is in use.
400  */
401 const unsigned char *vport_get_addr(const struct vport *vport)
402 {
403         return vport->ops->get_addr(vport);
404 }
405
406 /**
407  *      vport_get_kobj - retrieve associated kobj
408  *
409  * @vport: vport from which to retrieve the associated kobj
410  *
411  * Retrieves the associated kobj or null if no kobj.  The returned kobj is
412  * valid for as long as the vport exists.
413  */
414 struct kobject *vport_get_kobj(const struct vport *vport)
415 {
416         if (vport->ops->get_kobj)
417                 return vport->ops->get_kobj(vport);
418         else
419                 return NULL;
420 }
421
422 static int vport_call_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
423 {
424         int err;
425
426         rcu_read_lock();
427         err = vport->ops->get_stats(vport, stats);
428         rcu_read_unlock();
429
430         return err;
431 }
432
433 /**
434  *      vport_get_stats - retrieve device stats
435  *
436  * @vport: vport from which to retrieve the stats
437  * @stats: location to store stats
438  *
439  * Retrieves transmit, receive, and error stats for the given device.
440  *
441  * Must be called with RTNL lock or rcu_read_lock.
442  */
443 int vport_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
444 {
445         int i;
446
447         if (!(vport->ops->flags & VPORT_F_GEN_STATS))
448                 return vport_call_get_stats(vport, stats);
449
450         /* We potentially have 3 sources of stats that need to be
451          * combined: those we have collected (split into err_stats and
452          * percpu_stats), offset_stats from set_stats(), and device
453          * error stats from get_stats() (for errors that happen
454          * downstream and therefore aren't reported through our
455          * vport_record_error() function). */
456
457         spin_lock_bh(&vport->stats_lock);
458
459         *stats = vport->offset_stats;
460
461         stats->rx_errors        += vport->err_stats.rx_errors;
462         stats->tx_errors        += vport->err_stats.tx_errors;
463         stats->tx_dropped       += vport->err_stats.tx_dropped;
464         stats->rx_dropped       += vport->err_stats.rx_dropped;
465
466         spin_unlock_bh(&vport->stats_lock);
467
468         if (vport->ops->get_stats) {
469                 struct rtnl_link_stats64 dev_stats;
470                 int err;
471
472                 err = vport_call_get_stats(vport, &dev_stats);
473                 if (err)
474                         return err;
475
476                 stats->rx_errors           += dev_stats.rx_errors;
477                 stats->tx_errors           += dev_stats.tx_errors;
478                 stats->rx_dropped          += dev_stats.rx_dropped;
479                 stats->tx_dropped          += dev_stats.tx_dropped;
480                 stats->multicast           += dev_stats.multicast;
481                 stats->collisions          += dev_stats.collisions;
482                 stats->rx_length_errors    += dev_stats.rx_length_errors;
483                 stats->rx_over_errors      += dev_stats.rx_over_errors;
484                 stats->rx_crc_errors       += dev_stats.rx_crc_errors;
485                 stats->rx_frame_errors     += dev_stats.rx_frame_errors;
486                 stats->rx_fifo_errors      += dev_stats.rx_fifo_errors;
487                 stats->rx_missed_errors    += dev_stats.rx_missed_errors;
488                 stats->tx_aborted_errors   += dev_stats.tx_aborted_errors;
489                 stats->tx_carrier_errors   += dev_stats.tx_carrier_errors;
490                 stats->tx_fifo_errors      += dev_stats.tx_fifo_errors;
491                 stats->tx_heartbeat_errors += dev_stats.tx_heartbeat_errors;
492                 stats->tx_window_errors    += dev_stats.tx_window_errors;
493                 stats->rx_compressed       += dev_stats.rx_compressed;
494                 stats->tx_compressed       += dev_stats.tx_compressed;
495         }
496
497         for_each_possible_cpu(i) {
498                 const struct vport_percpu_stats *percpu_stats;
499                 struct vport_percpu_stats local_stats;
500                 unsigned seqcount;
501
502                 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
503
504                 do {
505                         seqcount = read_seqcount_begin(&percpu_stats->seqlock);
506                         local_stats = *percpu_stats;
507                 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
508
509                 stats->rx_bytes         += local_stats.rx_bytes;
510                 stats->rx_packets       += local_stats.rx_packets;
511                 stats->tx_bytes         += local_stats.tx_bytes;
512                 stats->tx_packets       += local_stats.tx_packets;
513         }
514
515         return 0;
516 }
517
518 /**
519  *      vport_get_flags - retrieve device flags
520  *
521  * @vport: vport from which to retrieve the flags
522  *
523  * Retrieves the flags of the given device.
524  *
525  * Must be called with RTNL lock or rcu_read_lock.
526  */
527 unsigned vport_get_flags(const struct vport *vport)
528 {
529         return vport->ops->get_dev_flags(vport);
530 }
531
532 /**
533  *      vport_get_flags - check whether device is running
534  *
535  * @vport: vport on which to check status.
536  *
537  * Checks whether the given device is running.
538  *
539  * Must be called with RTNL lock or rcu_read_lock.
540  */
541 int vport_is_running(const struct vport *vport)
542 {
543         return vport->ops->is_running(vport);
544 }
545
546 /**
547  *      vport_get_flags - retrieve device operating state
548  *
549  * @vport: vport from which to check status
550  *
551  * Retrieves the RFC2863 operstate of the given device.
552  *
553  * Must be called with RTNL lock or rcu_read_lock.
554  */
555 unsigned char vport_get_operstate(const struct vport *vport)
556 {
557         return vport->ops->get_operstate(vport);
558 }
559
560 /**
561  *      vport_get_ifindex - retrieve device system interface index
562  *
563  * @vport: vport from which to retrieve index
564  *
565  * Retrieves the system interface index of the given device or 0 if
566  * the device does not have one (in the case of virtual ports).
567  * Returns a negative index on error.
568  *
569  * Must be called with RTNL lock or rcu_read_lock.
570  */
571 int vport_get_ifindex(const struct vport *vport)
572 {
573         if (vport->ops->get_ifindex)
574                 return vport->ops->get_ifindex(vport);
575         else
576                 return 0;
577 }
578
579 /**
580  *      vport_get_iflink - retrieve device system link index
581  *
582  * @vport: vport from which to retrieve index
583  *
584  * Retrieves the system link index of the given device.  The link is the index
585  * of the interface on which the packet will actually be sent.  In most cases
586  * this is the same as the ifindex but may be different for tunnel devices.
587  * Returns a negative index on error.
588  *
589  * Must be called with RTNL lock or rcu_read_lock.
590  */
591 int vport_get_iflink(const struct vport *vport)
592 {
593         if (vport->ops->get_iflink)
594                 return vport->ops->get_iflink(vport);
595
596         /* If we don't have an iflink, use the ifindex.  In most cases they
597          * are the same. */
598         return vport_get_ifindex(vport);
599 }
600
601 /**
602  *      vport_get_mtu - retrieve device MTU
603  *
604  * @vport: vport from which to retrieve MTU
605  *
606  * Retrieves the MTU of the given device.  Returns 0 if @vport does not have an
607  * MTU (as e.g. some tunnels do not).  Either RTNL lock or rcu_read_lock must
608  * be held.
609  */
610 int vport_get_mtu(const struct vport *vport)
611 {
612         if (!vport->ops->get_mtu)
613                 return 0;
614         return vport->ops->get_mtu(vport);
615 }
616
617 /**
618  *      vport_get_options - retrieve device options
619  *
620  * @vport: vport from which to retrieve the options.
621  * @skb: sk_buff where options should be appended.
622  *
623  * Retrieves the configuration of the given device, appending an
624  * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
625  * vport-specific attributes to @skb.
626  *
627  * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
628  * negative error code if a real error occurred.  If an error occurs, @skb is
629  * left unmodified.
630  *
631  * Must be called with RTNL lock or rcu_read_lock.
632  */
633 int vport_get_options(const struct vport *vport, struct sk_buff *skb)
634 {
635         struct nlattr *nla;
636
637         nla = nla_nest_start(skb, OVS_VPORT_ATTR_OPTIONS);
638         if (!nla)
639                 return -EMSGSIZE;
640
641         if (vport->ops->get_options) {
642                 int err = vport->ops->get_options(vport, skb);
643                 if (err) {
644                         nla_nest_cancel(skb, nla);
645                         return err;
646                 }
647         }
648
649         nla_nest_end(skb, nla);
650         return 0;
651 }
652
653 /**
654  *      vport_receive - pass up received packet to the datapath for processing
655  *
656  * @vport: vport that received the packet
657  * @skb: skb that was received
658  *
659  * Must be called with rcu_read_lock.  The packet cannot be shared and
660  * skb->data should point to the Ethernet header.  The caller must have already
661  * called compute_ip_summed() to initialize the checksumming fields.
662  */
663 void vport_receive(struct vport *vport, struct sk_buff *skb)
664 {
665         if (vport->ops->flags & VPORT_F_GEN_STATS) {
666                 struct vport_percpu_stats *stats;
667
668                 local_bh_disable();
669                 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
670
671                 write_seqcount_begin(&stats->seqlock);
672                 stats->rx_packets++;
673                 stats->rx_bytes += skb->len;
674                 write_seqcount_end(&stats->seqlock);
675
676                 local_bh_enable();
677         }
678
679         if (!(vport->ops->flags & VPORT_F_FLOW))
680                 OVS_CB(skb)->flow = NULL;
681
682         if (!(vport->ops->flags & VPORT_F_TUN_ID))
683                 OVS_CB(skb)->tun_id = 0;
684
685         dp_process_received_packet(vport, skb);
686 }
687
688 /**
689  *      vport_send - send a packet on a device
690  *
691  * @vport: vport on which to send the packet
692  * @skb: skb to send
693  *
694  * Sends the given packet and returns the length of data sent.  Either RTNL
695  * lock or rcu_read_lock must be held.
696  */
697 int vport_send(struct vport *vport, struct sk_buff *skb)
698 {
699         int sent = vport->ops->send(vport, skb);
700
701         if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
702                 struct vport_percpu_stats *stats;
703
704                 local_bh_disable();
705                 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
706
707                 write_seqcount_begin(&stats->seqlock);
708                 stats->tx_packets++;
709                 stats->tx_bytes += sent;
710                 write_seqcount_end(&stats->seqlock);
711
712                 local_bh_enable();
713         }
714
715         return sent;
716 }
717
718 /**
719  *      vport_record_error - indicate device error to generic stats layer
720  *
721  * @vport: vport that encountered the error
722  * @err_type: one of enum vport_err_type types to indicate the error type
723  *
724  * If using the vport generic stats layer indicate that an error of the given
725  * type has occured.
726  */
727 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
728 {
729         if (vport->ops->flags & VPORT_F_GEN_STATS) {
730
731                 spin_lock_bh(&vport->stats_lock);
732
733                 switch (err_type) {
734                 case VPORT_E_RX_DROPPED:
735                         vport->err_stats.rx_dropped++;
736                         break;
737
738                 case VPORT_E_RX_ERROR:
739                         vport->err_stats.rx_errors++;
740                         break;
741
742                 case VPORT_E_TX_DROPPED:
743                         vport->err_stats.tx_dropped++;
744                         break;
745
746                 case VPORT_E_TX_ERROR:
747                         vport->err_stats.tx_errors++;
748                         break;
749                 };
750
751                 spin_unlock_bh(&vport->stats_lock);
752         }
753 }