datapath: Always use generic stats for devices (vports)
[sliver-openvswitch.git] / datapath / vport-netdev.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/if_arp.h>
12 #include <linux/if_bridge.h>
13 #include <linux/if_vlan.h>
14 #include <linux/kernel.h>
15 #include <linux/llc.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/skbuff.h>
18
19 #include <net/llc.h>
20
21 #include "checksum.h"
22 #include "datapath.h"
23 #include "vlan.h"
24 #include "vport-internal_dev.h"
25 #include "vport-netdev.h"
26
27 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) && \
28     !defined(HAVE_VLAN_BUG_WORKAROUND)
29 #include <linux/module.h>
30
31 static int vlan_tso __read_mostly = 0;
32 module_param(vlan_tso, int, 0644);
33 MODULE_PARM_DESC(vlan_tso, "Enable TSO for VLAN packets");
34 #else
35 #define vlan_tso true
36 #endif
37
38 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb);
39
40 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
41 /* Called with rcu_read_lock and bottom-halves disabled. */
42 static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
43 {
44         struct sk_buff *skb = *pskb;
45         struct vport *vport;
46
47         if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
48                 return RX_HANDLER_PASS;
49
50         vport = netdev_get_vport(skb->dev);
51
52         netdev_port_receive(vport, skb);
53
54         return RX_HANDLER_CONSUMED;
55 }
56 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
57 /* Called with rcu_read_lock and bottom-halves disabled. */
58 static struct sk_buff *netdev_frame_hook(struct sk_buff *skb)
59 {
60         struct vport *vport;
61
62         if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
63                 return skb;
64
65         vport = netdev_get_vport(skb->dev);
66
67         netdev_port_receive(vport, skb);
68
69         return NULL;
70 }
71 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
72 /*
73  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
74  * different set of devices!)
75  */
76 /* Called with rcu_read_lock and bottom-halves disabled. */
77 static struct sk_buff *netdev_frame_hook(struct net_bridge_port *p,
78                                          struct sk_buff *skb)
79 {
80         netdev_port_receive((struct vport *)p, skb);
81         return NULL;
82 }
83 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
84 /*
85  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
86  * different set of devices!)
87  */
88 /* Called with rcu_read_lock and bottom-halves disabled. */
89 static int netdev_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
90 {
91         netdev_port_receive((struct vport *)p, *pskb);
92         return 1;
93 }
94 #else
95 #error
96 #endif
97
98 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
99 static int netdev_init(void) { return 0; }
100 static void netdev_exit(void) { }
101 #else
102 static int netdev_init(void)
103 {
104         /* Hook into callback used by the bridge to intercept packets.
105          * Parasites we are. */
106         br_handle_frame_hook = netdev_frame_hook;
107
108         return 0;
109 }
110
111 static void netdev_exit(void)
112 {
113         br_handle_frame_hook = NULL;
114 }
115 #endif
116
117 static struct vport *netdev_create(const struct vport_parms *parms)
118 {
119         struct vport *vport;
120         struct netdev_vport *netdev_vport;
121         int err;
122
123         vport = vport_alloc(sizeof(struct netdev_vport), &netdev_vport_ops, parms);
124         if (IS_ERR(vport)) {
125                 err = PTR_ERR(vport);
126                 goto error;
127         }
128
129         netdev_vport = netdev_vport_priv(vport);
130
131         netdev_vport->dev = dev_get_by_name(&init_net, parms->name);
132         if (!netdev_vport->dev) {
133                 err = -ENODEV;
134                 goto error_free_vport;
135         }
136
137         if (netdev_vport->dev->flags & IFF_LOOPBACK ||
138             netdev_vport->dev->type != ARPHRD_ETHER ||
139             is_internal_dev(netdev_vport->dev)) {
140                 err = -EINVAL;
141                 goto error_put;
142         }
143
144         err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
145                                          vport);
146         if (err)
147                 goto error_put;
148
149         dev_set_promiscuity(netdev_vport->dev, 1);
150 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
151         dev_disable_lro(netdev_vport->dev);
152 #endif
153         netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
154
155         return vport;
156
157 error_put:
158         dev_put(netdev_vport->dev);
159 error_free_vport:
160         vport_free(vport);
161 error:
162         return ERR_PTR(err);
163 }
164
165 static void netdev_destroy(struct vport *vport)
166 {
167         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
168
169         netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
170         netdev_rx_handler_unregister(netdev_vport->dev);
171         dev_set_promiscuity(netdev_vport->dev, -1);
172
173         synchronize_rcu();
174
175         dev_put(netdev_vport->dev);
176         vport_free(vport);
177 }
178
179 int netdev_set_addr(struct vport *vport, const unsigned char *addr)
180 {
181         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
182         struct sockaddr sa;
183
184         sa.sa_family = ARPHRD_ETHER;
185         memcpy(sa.sa_data, addr, ETH_ALEN);
186
187         return dev_set_mac_address(netdev_vport->dev, &sa);
188 }
189
190 const char *netdev_get_name(const struct vport *vport)
191 {
192         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
193         return netdev_vport->dev->name;
194 }
195
196 const unsigned char *netdev_get_addr(const struct vport *vport)
197 {
198         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
199         return netdev_vport->dev->dev_addr;
200 }
201
202 struct kobject *netdev_get_kobj(const struct vport *vport)
203 {
204         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
205         return &netdev_vport->dev->NETDEV_DEV_MEMBER.kobj;
206 }
207
208 unsigned netdev_get_dev_flags(const struct vport *vport)
209 {
210         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
211         return dev_get_flags(netdev_vport->dev);
212 }
213
214 int netdev_is_running(const struct vport *vport)
215 {
216         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
217         return netif_running(netdev_vport->dev);
218 }
219
220 unsigned char netdev_get_operstate(const struct vport *vport)
221 {
222         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
223         return netdev_vport->dev->operstate;
224 }
225
226 int netdev_get_ifindex(const struct vport *vport)
227 {
228         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
229         return netdev_vport->dev->ifindex;
230 }
231
232 int netdev_get_mtu(const struct vport *vport)
233 {
234         const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
235         return netdev_vport->dev->mtu;
236 }
237
238 /* Must be called with rcu_read_lock. */
239 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
240 {
241         if (unlikely(!vport)) {
242                 kfree_skb(skb);
243                 return;
244         }
245
246         /* Make our own copy of the packet.  Otherwise we will mangle the
247          * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
248          * (No one comes after us, since we tell handle_bridge() that we took
249          * the packet.) */
250         skb = skb_share_check(skb, GFP_ATOMIC);
251         if (unlikely(!skb))
252                 return;
253
254         skb_push(skb, ETH_HLEN);
255
256         if (unlikely(compute_ip_summed(skb, false))) {
257                 kfree_skb(skb);
258                 return;
259         }
260         vlan_copy_skb_tci(skb);
261
262         vport_receive(vport, skb);
263 }
264
265 static inline unsigned packet_length(const struct sk_buff *skb)
266 {
267         unsigned length = skb->len - ETH_HLEN;
268
269         if (skb->protocol == htons(ETH_P_8021Q))
270                 length -= VLAN_HLEN;
271
272         return length;
273 }
274
275 static bool dev_supports_vlan_tx(struct net_device *dev)
276 {
277 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
278         /* Software fallback means every device supports vlan_tci on TX. */
279         return true;
280 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
281         return dev->features & NETIF_F_HW_VLAN_TX;
282 #else
283         /* Assume that the driver is buggy. */
284         return false;
285 #endif
286 }
287
288 static int netdev_send(struct vport *vport, struct sk_buff *skb)
289 {
290         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
291         int mtu = netdev_vport->dev->mtu;
292         int len;
293
294         if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
295                 if (net_ratelimit())
296                         pr_warn("%s: dropped over-mtu packet: %d > %d\n",
297                                 dp_name(vport->dp), packet_length(skb), mtu);
298                 goto error;
299         }
300
301         if (unlikely(skb_warn_if_lro(skb)))
302                 goto error;
303
304         skb->dev = netdev_vport->dev;
305         forward_ip_summed(skb, true);
306
307         if (vlan_tx_tag_present(skb) && !dev_supports_vlan_tx(skb->dev)) {
308                 int features;
309
310                 features = netif_skb_features(skb);
311
312                 if (!vlan_tso)
313                         features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
314                                       NETIF_F_UFO | NETIF_F_FSO);
315
316                 if (netif_needs_gso(skb, features)) {
317                         struct sk_buff *nskb;
318
319                         nskb = skb_gso_segment(skb, features);
320                         if (!nskb) {
321                                 if (unlikely(skb_cloned(skb) &&
322                                     pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) {
323                                         kfree_skb(skb);
324                                         return 0;
325                                 }
326
327                                 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
328                                 goto tag;
329                         }
330
331                         if (IS_ERR(nskb)) {
332                                 kfree_skb(skb);
333                                 return 0;
334                         }
335                         consume_skb(skb);
336                         skb = nskb;
337
338                         len = 0;
339                         do {
340                                 nskb = skb->next;
341                                 skb->next = NULL;
342
343                                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
344                                 if (likely(skb)) {
345                                         len += skb->len;
346                                         vlan_set_tci(skb, 0);
347                                         dev_queue_xmit(skb);
348                                 }
349
350                                 skb = nskb;
351                         } while (skb);
352
353                         return len;
354                 }
355
356 tag:
357                 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
358                 if (unlikely(!skb))
359                         return 0;
360                 vlan_set_tci(skb, 0);
361         }
362
363         len = skb->len;
364         dev_queue_xmit(skb);
365
366         return len;
367
368 error:
369         kfree_skb(skb);
370         vport_record_error(vport, VPORT_E_TX_DROPPED);
371         return 0;
372 }
373
374 /* Returns null if this device is not attached to a datapath. */
375 struct vport *netdev_get_vport(struct net_device *dev)
376 {
377 #ifdef IFF_BRIDGE_PORT
378 #if IFF_BRIDGE_PORT != IFF_OVS_DATAPATH
379         if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
380 #else
381         if (likely(rcu_access_pointer(dev->rx_handler) == netdev_frame_hook))   
382 #endif
383                 return (struct vport *)rcu_dereference_rtnl(dev->rx_handler_data);
384         else
385                 return NULL;
386 #else
387         return (struct vport *)rcu_dereference_rtnl(dev->br_port);
388 #endif
389 }
390
391 const struct vport_ops netdev_vport_ops = {
392         .type           = OVS_VPORT_TYPE_NETDEV,
393         .flags          = VPORT_F_REQUIRED,
394         .init           = netdev_init,
395         .exit           = netdev_exit,
396         .create         = netdev_create,
397         .destroy        = netdev_destroy,
398         .set_addr       = netdev_set_addr,
399         .get_name       = netdev_get_name,
400         .get_addr       = netdev_get_addr,
401         .get_kobj       = netdev_get_kobj,
402         .get_dev_flags  = netdev_get_dev_flags,
403         .is_running     = netdev_is_running,
404         .get_operstate  = netdev_get_operstate,
405         .get_ifindex    = netdev_get_ifindex,
406         .get_mtu        = netdev_get_mtu,
407         .send           = netdev_send,
408 };
409
410 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
411 /*
412  * In kernels earlier than 2.6.36, Open vSwitch cannot safely coexist with
413  * the Linux bridge module on any released version of Linux, because there
414  * is only a single bridge hook function and only a single br_port member
415  * in struct net_device.
416  *
417  * Declaring and exporting this symbol enforces mutual exclusion.  The bridge
418  * module also exports the same symbol, so the module loader will refuse to
419  * load both modules at the same time (e.g. "bridge: exports duplicate symbol
420  * br_should_route_hook (owned by openvswitch_mod)").
421  *
422  * The use of "typeof" here avoids the need to track changes in the type of
423  * br_should_route_hook over various kernel versions.
424  */
425 typeof(br_should_route_hook) br_should_route_hook;
426 EXPORT_SYMBOL(br_should_route_hook);
427 #endif