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