datapath: Fix recv path for CONFIG_PREEMPT_RCU.
[sliver-openvswitch.git] / datapath / vport-internal_dev.c
1 /*
2  * Copyright (c) 2009, 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/hardirq.h>
10 #include <linux/if_vlan.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/skbuff.h>
16 #include <linux/version.h>
17
18 #include "checksum.h"
19 #include "datapath.h"
20 #include "vlan.h"
21 #include "vport-generic.h"
22 #include "vport-internal_dev.h"
23 #include "vport-netdev.h"
24
25 struct internal_dev {
26         struct vport *vport;
27         struct net_device_stats stats;
28 };
29
30 static inline struct internal_dev *internal_dev_priv(struct net_device *netdev)
31 {
32         return netdev_priv(netdev);
33 }
34
35 /* This function is only called by the kernel network layer.*/
36 static struct net_device_stats *internal_dev_sys_stats(struct net_device *netdev)
37 {
38         struct vport *vport = internal_dev_get_vport(netdev);
39         struct net_device_stats *stats = &internal_dev_priv(netdev)->stats;
40
41         if (vport) {
42                 struct ovs_vport_stats vport_stats;
43
44                 vport_get_stats(vport, &vport_stats);
45
46                 /* The tx and rx stats need to be swapped because the switch
47                  * and host OS have opposite perspectives. */
48                 stats->rx_packets       = vport_stats.tx_packets;
49                 stats->tx_packets       = vport_stats.rx_packets;
50                 stats->rx_bytes         = vport_stats.tx_bytes;
51                 stats->tx_bytes         = vport_stats.rx_bytes;
52                 stats->rx_errors        = vport_stats.tx_errors;
53                 stats->tx_errors        = vport_stats.rx_errors;
54                 stats->rx_dropped       = vport_stats.tx_dropped;
55                 stats->tx_dropped       = vport_stats.rx_dropped;
56         }
57
58         return stats;
59 }
60
61 static int internal_dev_mac_addr(struct net_device *dev, void *p)
62 {
63         struct sockaddr *addr = p;
64
65         if (!is_valid_ether_addr(addr->sa_data))
66                 return -EADDRNOTAVAIL;
67         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
68         return 0;
69 }
70
71 /* Called with rcu_read_lock_bh. */
72 static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
73 {
74         if (unlikely(compute_ip_summed(skb, true))) {
75                 kfree_skb(skb);
76                 return 0;
77         }
78
79         vlan_copy_skb_tci(skb);
80         OVS_CB(skb)->flow = NULL;
81
82         rcu_read_lock();
83         vport_receive(internal_dev_priv(netdev)->vport, skb);
84         rcu_read_unlock();
85         return 0;
86 }
87
88 static int internal_dev_open(struct net_device *netdev)
89 {
90         netif_start_queue(netdev);
91         return 0;
92 }
93
94 static int internal_dev_stop(struct net_device *netdev)
95 {
96         netif_stop_queue(netdev);
97         return 0;
98 }
99
100 static void internal_dev_getinfo(struct net_device *netdev,
101                                  struct ethtool_drvinfo *info)
102 {
103         strcpy(info->driver, "openvswitch");
104 }
105
106 static const struct ethtool_ops internal_dev_ethtool_ops = {
107         .get_drvinfo    = internal_dev_getinfo,
108         .get_link       = ethtool_op_get_link,
109         .get_sg         = ethtool_op_get_sg,
110         .set_sg         = ethtool_op_set_sg,
111         .get_tx_csum    = ethtool_op_get_tx_csum,
112         .set_tx_csum    = ethtool_op_set_tx_hw_csum,
113         .get_tso        = ethtool_op_get_tso,
114         .set_tso        = ethtool_op_set_tso,
115 };
116
117 static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
118 {
119         if (new_mtu < 68)
120                 return -EINVAL;
121
122         netdev->mtu = new_mtu;
123         return 0;
124 }
125
126 static int internal_dev_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
127 {
128         if (dp_ioctl_hook)
129                 return dp_ioctl_hook(dev, ifr, cmd);
130
131         return -EOPNOTSUPP;
132 }
133
134 static void internal_dev_destructor(struct net_device *dev)
135 {
136         struct vport *vport = internal_dev_get_vport(dev);
137
138         vport_free(vport);
139         free_netdev(dev);
140 }
141
142 #ifdef HAVE_NET_DEVICE_OPS
143 static const struct net_device_ops internal_dev_netdev_ops = {
144         .ndo_open = internal_dev_open,
145         .ndo_stop = internal_dev_stop,
146         .ndo_start_xmit = internal_dev_xmit,
147         .ndo_set_mac_address = internal_dev_mac_addr,
148         .ndo_do_ioctl = internal_dev_do_ioctl,
149         .ndo_change_mtu = internal_dev_change_mtu,
150         .ndo_get_stats = internal_dev_sys_stats,
151 };
152 #endif
153
154 static void do_setup(struct net_device *netdev)
155 {
156         ether_setup(netdev);
157
158 #ifdef HAVE_NET_DEVICE_OPS
159         netdev->netdev_ops = &internal_dev_netdev_ops;
160 #else
161         netdev->do_ioctl = internal_dev_do_ioctl;
162         netdev->get_stats = internal_dev_sys_stats;
163         netdev->hard_start_xmit = internal_dev_xmit;
164         netdev->open = internal_dev_open;
165         netdev->stop = internal_dev_stop;
166         netdev->set_mac_address = internal_dev_mac_addr;
167         netdev->change_mtu = internal_dev_change_mtu;
168 #endif
169
170         netdev->destructor = internal_dev_destructor;
171         SET_ETHTOOL_OPS(netdev, &internal_dev_ethtool_ops);
172         netdev->tx_queue_len = 0;
173
174         netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
175         netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
176                                 NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_TSO;
177
178 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
179         netdev->vlan_features = netdev->features;
180         netdev->features |= NETIF_F_HW_VLAN_TX;
181 #endif
182
183         vport_gen_rand_ether_addr(netdev->dev_addr);
184 }
185
186 static struct vport *internal_dev_create(const struct vport_parms *parms)
187 {
188         struct vport *vport;
189         struct netdev_vport *netdev_vport;
190         struct internal_dev *internal_dev;
191         int err;
192
193         vport = vport_alloc(sizeof(struct netdev_vport), &internal_vport_ops, parms);
194         if (IS_ERR(vport)) {
195                 err = PTR_ERR(vport);
196                 goto error;
197         }
198
199         netdev_vport = netdev_vport_priv(vport);
200
201         netdev_vport->dev = alloc_netdev(sizeof(struct internal_dev), parms->name, do_setup);
202         if (!netdev_vport->dev) {
203                 err = -ENOMEM;
204                 goto error_free_vport;
205         }
206
207         internal_dev = internal_dev_priv(netdev_vport->dev);
208         internal_dev->vport = vport;
209
210         err = register_netdevice(netdev_vport->dev);
211         if (err)
212                 goto error_free_netdev;
213
214         dev_set_promiscuity(netdev_vport->dev, 1);
215         netif_start_queue(netdev_vport->dev);
216
217         return vport;
218
219 error_free_netdev:
220         free_netdev(netdev_vport->dev);
221 error_free_vport:
222         vport_free(vport);
223 error:
224         return ERR_PTR(err);
225 }
226
227 static void internal_dev_destroy(struct vport *vport)
228 {
229         struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
230
231         netif_stop_queue(netdev_vport->dev);
232         dev_set_promiscuity(netdev_vport->dev, -1);
233
234         /* unregister_netdevice() waits for an RCU grace period. */
235         unregister_netdevice(netdev_vport->dev);
236 }
237
238 static int internal_dev_recv(struct vport *vport, struct sk_buff *skb)
239 {
240         struct net_device *netdev = netdev_vport_priv(vport)->dev;
241         int len;
242
243 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
244         if (unlikely(vlan_deaccel_tag(skb)))
245                 return 0;
246 #endif
247
248         len = skb->len;
249         skb->dev = netdev;
250         skb->pkt_type = PACKET_HOST;
251         skb->protocol = eth_type_trans(skb, netdev);
252         forward_ip_summed(skb, false);
253
254         if (in_interrupt())
255                 netif_rx(skb);
256         else
257                 netif_rx_ni(skb);
258
259 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
260         netdev->last_rx = jiffies;
261 #endif
262
263         return len;
264 }
265
266 const struct vport_ops internal_vport_ops = {
267         .type           = OVS_VPORT_TYPE_INTERNAL,
268         .flags          = VPORT_F_REQUIRED | VPORT_F_FLOW,
269         .create         = internal_dev_create,
270         .destroy        = internal_dev_destroy,
271         .set_addr       = netdev_set_addr,
272         .get_name       = netdev_get_name,
273         .get_addr       = netdev_get_addr,
274         .get_kobj       = netdev_get_kobj,
275         .get_dev_flags  = netdev_get_dev_flags,
276         .is_running     = netdev_is_running,
277         .get_operstate  = netdev_get_operstate,
278         .get_ifindex    = netdev_get_ifindex,
279         .get_mtu        = netdev_get_mtu,
280         .send           = internal_dev_recv,
281 };
282
283 int is_internal_dev(const struct net_device *netdev)
284 {
285 #ifdef HAVE_NET_DEVICE_OPS
286         return netdev->netdev_ops == &internal_dev_netdev_ops;
287 #else
288         return netdev->open == internal_dev_open;
289 #endif
290 }
291
292 int is_internal_vport(const struct vport *vport)
293 {
294         return vport->ops == &internal_vport_ops;
295 }
296
297 struct vport *internal_dev_get_vport(struct net_device *netdev)
298 {
299         if (!is_internal_dev(netdev))
300                 return NULL;
301
302         return internal_dev_priv(netdev)->vport;
303 }