2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
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>
21 #include "vport-generic.h"
22 #include "vport-internal_dev.h"
23 #include "vport-netdev.h"
25 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
26 #define HAVE_NET_DEVICE_OPS
31 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
32 struct net_device_stats stats
;
36 static struct internal_dev
*internal_dev_priv(struct net_device
*netdev
)
38 return netdev_priv(netdev
);
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
)
47 static struct net_device_stats
*internal_dev_sys_stats(struct net_device
*netdev
)
49 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
50 struct net_device_stats
*stats
= &internal_dev_priv(netdev
)->stats
;
52 struct net_device_stats
*stats
= &netdev
->stats
;
55 struct vport
*vport
= internal_dev_get_vport(netdev
);
56 struct ovs_vport_stats vport_stats
;
58 vport_get_stats(vport
, &vport_stats
);
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
;
74 static int internal_dev_mac_addr(struct net_device
*dev
, void *p
)
76 struct sockaddr
*addr
= p
;
78 if (!is_valid_ether_addr(addr
->sa_data
))
79 return -EADDRNOTAVAIL
;
80 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
84 /* Called with rcu_read_lock_bh. */
85 static int internal_dev_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
87 if (unlikely(compute_ip_summed(skb
, true))) {
92 vlan_copy_skb_tci(skb
);
93 OVS_CB(skb
)->flow
= NULL
;
96 vport_receive(internal_dev_priv(netdev
)->vport
, skb
);
101 static int internal_dev_open(struct net_device
*netdev
)
103 netif_start_queue(netdev
);
107 static int internal_dev_stop(struct net_device
*netdev
)
109 netif_stop_queue(netdev
);
113 static void internal_dev_getinfo(struct net_device
*netdev
,
114 struct ethtool_drvinfo
*info
)
116 strcpy(info
->driver
, "openvswitch");
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
,
132 static int internal_dev_change_mtu(struct net_device
*netdev
, int new_mtu
)
137 netdev
->mtu
= new_mtu
;
141 static int internal_dev_do_ioctl(struct net_device
*dev
,
142 struct ifreq
*ifr
, int cmd
)
145 return dp_ioctl_hook(dev
, ifr
, cmd
);
150 static void internal_dev_destructor(struct net_device
*dev
)
152 struct vport
*vport
= internal_dev_get_vport(dev
);
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
,
169 .ndo_get_stats
= internal_dev_sys_stats
,
174 static void do_setup(struct net_device
*netdev
)
178 #ifdef HAVE_NET_DEVICE_OPS
179 netdev
->netdev_ops
= &internal_dev_netdev_ops
;
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
;
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;
195 netdev
->features
= NETIF_F_LLTX
| NETIF_F_SG
| NETIF_F_FRAGLIST
|
196 NETIF_F_HIGHDMA
| NETIF_F_HW_CSUM
| NETIF_F_TSO
;
198 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
199 netdev
->vlan_features
= netdev
->features
;
200 netdev
->features
|= NETIF_F_HW_VLAN_TX
;
203 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
204 netdev
->hw_features
= netdev
->features
& ~NETIF_F_LLTX
;
206 vport_gen_rand_ether_addr(netdev
->dev_addr
);
209 static struct vport
*internal_dev_create(const struct vport_parms
*parms
)
212 struct netdev_vport
*netdev_vport
;
213 struct internal_dev
*internal_dev
;
216 vport
= vport_alloc(sizeof(struct netdev_vport
),
217 &internal_vport_ops
, parms
);
219 err
= PTR_ERR(vport
);
223 netdev_vport
= netdev_vport_priv(vport
);
225 netdev_vport
->dev
= alloc_netdev(sizeof(struct internal_dev
),
226 parms
->name
, do_setup
);
227 if (!netdev_vport
->dev
) {
229 goto error_free_vport
;
232 internal_dev
= internal_dev_priv(netdev_vport
->dev
);
233 internal_dev
->vport
= vport
;
235 err
= register_netdevice(netdev_vport
->dev
);
237 goto error_free_netdev
;
239 dev_set_promiscuity(netdev_vport
->dev
, 1);
240 netif_start_queue(netdev_vport
->dev
);
245 free_netdev(netdev_vport
->dev
);
252 static void internal_dev_destroy(struct vport
*vport
)
254 struct netdev_vport
*netdev_vport
= netdev_vport_priv(vport
);
256 netif_stop_queue(netdev_vport
->dev
);
257 dev_set_promiscuity(netdev_vport
->dev
, -1);
259 /* unregister_netdevice() waits for an RCU grace period. */
260 unregister_netdevice(netdev_vport
->dev
);
263 static int internal_dev_recv(struct vport
*vport
, struct sk_buff
*skb
)
265 struct net_device
*netdev
= netdev_vport_priv(vport
)->dev
;
268 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
269 if (unlikely(vlan_deaccel_tag(skb
)))
275 skb
->pkt_type
= PACKET_HOST
;
276 skb
->protocol
= eth_type_trans(skb
, netdev
);
277 forward_ip_summed(skb
, false);
281 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
282 netdev
->last_rx
= jiffies
;
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
,
305 int is_internal_dev(const struct net_device
*netdev
)
307 #ifdef HAVE_NET_DEVICE_OPS
308 return netdev
->netdev_ops
== &internal_dev_netdev_ops
;
310 return netdev
->open
== internal_dev_open
;
314 int is_internal_vport(const struct vport
*vport
)
316 return vport
->ops
== &internal_vport_ops
;
319 struct vport
*internal_dev_get_vport(struct net_device
*netdev
)
321 if (!is_internal_dev(netdev
))
324 return internal_dev_priv(netdev
)->vport
;