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