2 * Copyright (c) 2009, 2010 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/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/rcupdate.h>
14 #include <linux/skbuff.h>
15 #include <linux/version.h>
18 #include "vport-generic.h"
19 #include "vport-internal_dev.h"
20 #include "vport-netdev.h"
23 struct vport *attached_vport, *vport;
24 struct net_device_stats stats;
27 static inline struct internal_dev *internal_dev_priv(struct net_device *netdev)
29 return netdev_priv(netdev);
32 /* This function is only called by the kernel network layer. It is not a vport
33 * get_stats() function. If a vport get_stats() function is defined that
34 * results in this being called it will cause infinite recursion. */
35 static struct net_device_stats *internal_dev_sys_stats(struct net_device *netdev)
37 struct vport *vport = internal_dev_get_vport(netdev);
38 struct net_device_stats *stats = &internal_dev_priv(netdev)->stats;
41 struct rtnl_link_stats64 vport_stats;
43 vport_get_stats(vport, &vport_stats);
45 /* The tx and rx stats need to be swapped because the switch
46 * and host OS have opposite perspectives. */
47 stats->rx_packets = vport_stats.tx_packets;
48 stats->tx_packets = vport_stats.rx_packets;
49 stats->rx_bytes = vport_stats.tx_bytes;
50 stats->tx_bytes = vport_stats.rx_bytes;
51 stats->rx_errors = vport_stats.tx_errors;
52 stats->tx_errors = vport_stats.rx_errors;
53 stats->rx_dropped = vport_stats.tx_dropped;
54 stats->tx_dropped = vport_stats.rx_dropped;
55 stats->collisions = vport_stats.collisions;
61 static int internal_dev_mac_addr(struct net_device *dev, void *p)
63 struct sockaddr *addr = p;
65 if (!is_valid_ether_addr(addr->sa_data))
66 return -EADDRNOTAVAIL;
67 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
71 /* Called with rcu_read_lock and bottom-halves disabled. */
72 static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
74 struct internal_dev *internal_dev = internal_dev_priv(netdev);
75 struct vport *vport = rcu_dereference(internal_dev->vport);
77 compute_ip_summed(skb, true);
78 OVS_CB(skb)->flow = NULL;
80 vport_receive(vport, skb);
84 static int internal_dev_open(struct net_device *netdev)
86 netif_start_queue(netdev);
90 static int internal_dev_stop(struct net_device *netdev)
92 netif_stop_queue(netdev);
96 static void internal_dev_getinfo(struct net_device *netdev,
97 struct ethtool_drvinfo *info)
99 struct vport *vport = internal_dev_get_vport(netdev);
100 struct dp_port *dp_port;
102 strcpy(info->driver, "openvswitch");
107 dp_port = vport_get_dp_port(vport);
109 sprintf(info->bus_info, "%d.%d", dp_port->dp->dp_idx, dp_port->port_no);
112 static const struct ethtool_ops internal_dev_ethtool_ops = {
113 .get_drvinfo = internal_dev_getinfo,
114 .get_link = ethtool_op_get_link,
115 .get_sg = ethtool_op_get_sg,
116 .set_sg = ethtool_op_set_sg,
117 .get_tx_csum = ethtool_op_get_tx_csum,
118 .set_tx_csum = ethtool_op_set_tx_hw_csum,
119 .get_tso = ethtool_op_get_tso,
120 .set_tso = ethtool_op_set_tso,
123 static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
125 struct vport *vport = internal_dev_get_vport(netdev);
131 struct dp_port *dp_port = vport_get_dp_port(vport);
134 if (new_mtu > dp_min_mtu(dp_port->dp))
139 netdev->mtu = new_mtu;
143 static int internal_dev_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
146 return dp_ioctl_hook(dev, ifr, cmd);
151 #ifdef HAVE_NET_DEVICE_OPS
152 static const struct net_device_ops internal_dev_netdev_ops = {
153 .ndo_open = internal_dev_open,
154 .ndo_stop = internal_dev_stop,
155 .ndo_start_xmit = internal_dev_xmit,
156 .ndo_set_mac_address = internal_dev_mac_addr,
157 .ndo_do_ioctl = internal_dev_do_ioctl,
158 .ndo_change_mtu = internal_dev_change_mtu,
159 .ndo_get_stats = internal_dev_sys_stats,
163 static void do_setup(struct net_device *netdev)
167 #ifdef HAVE_NET_DEVICE_OPS
168 netdev->netdev_ops = &internal_dev_netdev_ops;
170 netdev->do_ioctl = internal_dev_do_ioctl;
171 netdev->get_stats = internal_dev_sys_stats;
172 netdev->hard_start_xmit = internal_dev_xmit;
173 netdev->open = internal_dev_open;
174 netdev->stop = internal_dev_stop;
175 netdev->set_mac_address = internal_dev_mac_addr;
176 netdev->change_mtu = internal_dev_change_mtu;
179 netdev->destructor = free_netdev;
180 SET_ETHTOOL_OPS(netdev, &internal_dev_ethtool_ops);
181 netdev->tx_queue_len = 0;
183 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
184 netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
185 NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_TSO;
187 vport_gen_rand_ether_addr(netdev->dev_addr);
190 static struct vport *internal_dev_create(const char *name,
191 const void __user *config)
194 struct netdev_vport *netdev_vport;
195 struct internal_dev *internal_dev;
198 vport = vport_alloc(sizeof(struct netdev_vport), &internal_vport_ops);
200 err = PTR_ERR(vport);
204 netdev_vport = netdev_vport_priv(vport);
206 netdev_vport->dev = alloc_netdev(sizeof(struct internal_dev), name, do_setup);
207 if (!netdev_vport->dev) {
209 goto error_free_vport;
212 internal_dev = internal_dev_priv(netdev_vport->dev);
213 rcu_assign_pointer(internal_dev->vport, vport);
215 err = register_netdevice(netdev_vport->dev);
217 goto error_free_netdev;
222 free_netdev(netdev_vport->dev);
229 static int internal_dev_destroy(struct vport *vport)
231 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
233 unregister_netdevice(netdev_vport->dev);
239 static int internal_dev_attach(struct vport *vport)
241 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
242 struct internal_dev *internal_dev = internal_dev_priv(netdev_vport->dev);
244 rcu_assign_pointer(internal_dev->attached_vport, internal_dev->vport);
245 dev_set_promiscuity(netdev_vport->dev, 1);
246 netif_start_queue(netdev_vport->dev);
251 static int internal_dev_detach(struct vport *vport)
253 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
254 struct internal_dev *internal_dev = internal_dev_priv(netdev_vport->dev);
256 netif_stop_queue(netdev_vport->dev);
257 dev_set_promiscuity(netdev_vport->dev, -1);
258 rcu_assign_pointer(internal_dev->attached_vport, NULL);
263 static int internal_dev_recv(struct vport *vport, struct sk_buff *skb)
265 struct net_device *netdev = netdev_vport_priv(vport)->dev;
270 skb->pkt_type = PACKET_HOST;
271 skb->protocol = eth_type_trans(skb, netdev);
278 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
279 netdev->last_rx = jiffies;
285 const struct vport_ops internal_vport_ops = {
287 .flags = VPORT_F_REQUIRED | VPORT_F_GEN_STATS | VPORT_F_FLOW,
288 .create = internal_dev_create,
289 .destroy = internal_dev_destroy,
290 .attach = internal_dev_attach,
291 .detach = internal_dev_detach,
292 .set_mtu = netdev_set_mtu,
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_iflink = netdev_get_iflink,
302 .get_mtu = netdev_get_mtu,
303 .send = internal_dev_recv,
306 int is_internal_dev(const struct net_device *netdev)
308 #ifdef HAVE_NET_DEVICE_OPS
309 return netdev->netdev_ops == &internal_dev_netdev_ops;
311 return netdev->open == internal_dev_open;
315 int is_internal_vport(const struct vport *vport)
317 return vport->ops == &internal_vport_ops;
320 struct vport *internal_dev_get_vport(struct net_device *netdev)
322 struct internal_dev *internal_dev;
324 if (!is_internal_dev(netdev))
327 internal_dev = internal_dev_priv(netdev);
328 return rcu_dereference(internal_dev->attached_vport);