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"
27 struct net_device_stats stats;
30 static inline struct internal_dev *internal_dev_priv(struct net_device *netdev)
32 return netdev_priv(netdev);
35 /* This function is only called by the kernel network layer. It is not a vport
36 * get_stats() function. If a vport get_stats() function is defined that
37 * results in this being called it will cause infinite recursion. */
38 static struct net_device_stats *internal_dev_sys_stats(struct net_device *netdev)
40 struct vport *vport = internal_dev_get_vport(netdev);
41 struct net_device_stats *stats = &internal_dev_priv(netdev)->stats;
44 struct rtnl_link_stats64 vport_stats;
46 vport_get_stats(vport, &vport_stats);
48 /* The tx and rx stats need to be swapped because the switch
49 * and host OS have opposite perspectives. */
50 stats->rx_packets = vport_stats.tx_packets;
51 stats->tx_packets = vport_stats.rx_packets;
52 stats->rx_bytes = vport_stats.tx_bytes;
53 stats->tx_bytes = vport_stats.rx_bytes;
54 stats->rx_errors = vport_stats.tx_errors;
55 stats->tx_errors = vport_stats.rx_errors;
56 stats->rx_dropped = vport_stats.tx_dropped;
57 stats->tx_dropped = vport_stats.rx_dropped;
58 stats->collisions = vport_stats.collisions;
64 static int internal_dev_mac_addr(struct net_device *dev, void *p)
66 struct sockaddr *addr = p;
68 if (!is_valid_ether_addr(addr->sa_data))
69 return -EADDRNOTAVAIL;
70 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
74 /* Called with rcu_read_lock and bottom-halves disabled. */
75 static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
77 if (unlikely(compute_ip_summed(skb, true))) {
82 vlan_copy_skb_tci(skb);
83 OVS_CB(skb)->flow = NULL;
85 vport_receive(internal_dev_priv(netdev)->vport, skb);
89 static int internal_dev_open(struct net_device *netdev)
91 netif_start_queue(netdev);
95 static int internal_dev_stop(struct net_device *netdev)
97 netif_stop_queue(netdev);
101 static void internal_dev_getinfo(struct net_device *netdev,
102 struct ethtool_drvinfo *info)
104 strcpy(info->driver, "openvswitch");
107 static const struct ethtool_ops internal_dev_ethtool_ops = {
108 .get_drvinfo = internal_dev_getinfo,
109 .get_link = ethtool_op_get_link,
110 .get_sg = ethtool_op_get_sg,
111 .set_sg = ethtool_op_set_sg,
112 .get_tx_csum = ethtool_op_get_tx_csum,
113 .set_tx_csum = ethtool_op_set_tx_hw_csum,
114 .get_tso = ethtool_op_get_tso,
115 .set_tso = ethtool_op_set_tso,
118 static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
120 struct vport *vport = internal_dev_get_vport(netdev);
125 if (new_mtu > dp_min_mtu(vport->dp))
128 netdev->mtu = new_mtu;
132 static int internal_dev_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
135 return dp_ioctl_hook(dev, ifr, cmd);
140 static void internal_dev_destructor(struct net_device *dev)
142 struct vport *vport = internal_dev_get_vport(dev);
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,
160 static void do_setup(struct net_device *netdev)
164 #ifdef HAVE_NET_DEVICE_OPS
165 netdev->netdev_ops = &internal_dev_netdev_ops;
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;
176 netdev->destructor = internal_dev_destructor;
177 SET_ETHTOOL_OPS(netdev, &internal_dev_ethtool_ops);
178 netdev->tx_queue_len = 0;
180 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
181 netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
182 NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_TSO;
184 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
185 netdev->vlan_features = netdev->features;
186 netdev->features |= NETIF_F_HW_VLAN_TX;
189 vport_gen_rand_ether_addr(netdev->dev_addr);
192 static struct vport *internal_dev_create(const struct vport_parms *parms)
195 struct netdev_vport *netdev_vport;
196 struct internal_dev *internal_dev;
199 vport = vport_alloc(sizeof(struct netdev_vport), &internal_vport_ops, parms);
201 err = PTR_ERR(vport);
205 netdev_vport = netdev_vport_priv(vport);
207 netdev_vport->dev = alloc_netdev(sizeof(struct internal_dev), parms->name, do_setup);
208 if (!netdev_vport->dev) {
210 goto error_free_vport;
213 internal_dev = internal_dev_priv(netdev_vport->dev);
214 internal_dev->vport = vport;
216 err = register_netdevice(netdev_vport->dev);
218 goto error_free_netdev;
220 dev_set_promiscuity(netdev_vport->dev, 1);
221 netif_start_queue(netdev_vport->dev);
226 free_netdev(netdev_vport->dev);
233 static int internal_dev_destroy(struct vport *vport)
235 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
237 netif_stop_queue(netdev_vport->dev);
238 dev_set_promiscuity(netdev_vport->dev, -1);
240 /* unregister_netdevice() waits for an RCU grace period. */
241 unregister_netdevice(netdev_vport->dev);
246 static int internal_dev_recv(struct vport *vport, struct sk_buff *skb)
248 struct net_device *netdev = netdev_vport_priv(vport)->dev;
251 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
252 if (unlikely(vlan_deaccel_tag(skb)))
258 skb->pkt_type = PACKET_HOST;
259 skb->protocol = eth_type_trans(skb, netdev);
260 forward_ip_summed(skb, false);
267 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
268 netdev->last_rx = jiffies;
274 const struct vport_ops internal_vport_ops = {
275 .type = ODP_VPORT_TYPE_INTERNAL,
276 .flags = VPORT_F_REQUIRED | VPORT_F_GEN_STATS | VPORT_F_FLOW,
277 .create = internal_dev_create,
278 .destroy = internal_dev_destroy,
279 .set_mtu = netdev_set_mtu,
280 .set_addr = netdev_set_addr,
281 .get_name = netdev_get_name,
282 .get_addr = netdev_get_addr,
283 .get_kobj = netdev_get_kobj,
284 .get_dev_flags = netdev_get_dev_flags,
285 .is_running = netdev_is_running,
286 .get_operstate = netdev_get_operstate,
287 .get_ifindex = netdev_get_ifindex,
288 .get_iflink = netdev_get_iflink,
289 .get_mtu = netdev_get_mtu,
290 .send = internal_dev_recv,
293 int is_internal_dev(const struct net_device *netdev)
295 #ifdef HAVE_NET_DEVICE_OPS
296 return netdev->netdev_ops == &internal_dev_netdev_ops;
298 return netdev->open == internal_dev_open;
302 int is_internal_vport(const struct vport *vport)
304 return vport->ops == &internal_vport_ops;
307 struct vport *internal_dev_get_vport(struct net_device *netdev)
309 if (!is_internal_dev(netdev))
312 return internal_dev_priv(netdev)->vport;