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/percpu.h>
14 #include <linux/rcupdate.h>
15 #include <linux/skbuff.h>
18 #include "openvswitch/internal_dev.h"
19 #include "vport-generic.h"
20 #include "vport-internal_dev.h"
21 #include "vport-netdev.h"
24 unsigned long rx_packets;
25 unsigned long rx_bytes;
26 unsigned long tx_packets;
27 unsigned long tx_bytes;
33 struct net_device_stats stats;
34 struct pcpu_lstats *lstats;
36 /* This is warty support for XAPI, which does not support summing bond
37 * device statistics itself. 'extra_stats' can be set by userspace via
38 * the DP_DEV_SET_STATS ioctl and, if they are, then they are added to
39 * the real device stats. */
40 struct pcpu_lstats extra_stats;
43 static inline struct internal_dev *internal_dev_priv(struct net_device *netdev)
45 return netdev_priv(netdev);
48 static struct net_device_stats *internal_dev_get_stats(struct net_device *netdev)
50 struct internal_dev *internal_dev = internal_dev_priv(netdev);
51 struct net_device_stats *stats;
54 stats = &internal_dev->stats;
55 stats->rx_bytes = internal_dev->extra_stats.rx_bytes;
56 stats->rx_packets = internal_dev->extra_stats.rx_packets;
57 stats->tx_bytes = internal_dev->extra_stats.tx_bytes;
58 stats->tx_packets = internal_dev->extra_stats.tx_packets;
59 for_each_possible_cpu(i) {
60 const struct pcpu_lstats *lb_stats;
62 lb_stats = per_cpu_ptr(internal_dev->lstats, i);
63 stats->rx_bytes += lb_stats->rx_bytes;
64 stats->rx_packets += lb_stats->rx_packets;
65 stats->tx_bytes += lb_stats->tx_bytes;
66 stats->tx_packets += lb_stats->tx_packets;
71 static int internal_dev_mac_addr(struct net_device *dev, void *p)
73 struct sockaddr *addr = p;
75 if (!is_valid_ether_addr(addr->sa_data))
76 return -EADDRNOTAVAIL;
77 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
81 /* Called with rcu_read_lock and bottom-halves disabled. */
82 static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
84 struct internal_dev *internal_dev = internal_dev_priv(netdev);
85 struct vport *vport = internal_dev_get_vport(netdev);
86 struct pcpu_lstats *lb_stats;
88 /* We need our own clone. */
89 skb = skb_share_check(skb, GFP_ATOMIC);
93 lb_stats = per_cpu_ptr(internal_dev->lstats, smp_processor_id());
94 lb_stats->tx_packets++;
95 lb_stats->tx_bytes += skb->len;
97 skb_reset_mac_header(skb);
98 compute_ip_summed(skb, true);
100 vport_receive(vport, skb);
105 static int internal_dev_open(struct net_device *netdev)
107 netif_start_queue(netdev);
111 static int internal_dev_stop(struct net_device *netdev)
113 netif_stop_queue(netdev);
117 static void internal_dev_getinfo(struct net_device *netdev,
118 struct ethtool_drvinfo *info)
120 struct dp_port *dp_port = vport_get_dp_port(internal_dev_get_vport(netdev));
122 strcpy(info->driver, "openvswitch");
124 sprintf(info->bus_info, "%d.%d", dp_port->dp->dp_idx, dp_port->port_no);
127 static struct ethtool_ops internal_dev_ethtool_ops = {
128 .get_drvinfo = internal_dev_getinfo,
129 .get_link = ethtool_op_get_link,
130 .get_sg = ethtool_op_get_sg,
131 .set_sg = ethtool_op_set_sg,
132 .get_tx_csum = ethtool_op_get_tx_csum,
133 .set_tx_csum = ethtool_op_set_tx_hw_csum,
134 .get_tso = ethtool_op_get_tso,
135 .set_tso = ethtool_op_set_tso,
138 static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
140 struct dp_port *dp_port = vport_get_dp_port(internal_dev_get_vport(netdev));
146 if (new_mtu > dp_min_mtu(dp_port->dp))
150 netdev->mtu = new_mtu;
154 static int internal_dev_init(struct net_device *netdev)
156 struct internal_dev *internal_dev = internal_dev_priv(netdev);
158 internal_dev->lstats = alloc_percpu(struct pcpu_lstats);
159 if (!internal_dev->lstats)
165 static void internal_dev_free(struct net_device *netdev)
167 struct internal_dev *internal_dev = internal_dev_priv(netdev);
169 free_percpu(internal_dev->lstats);
173 static int internal_dev_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
175 struct internal_dev *internal_dev = internal_dev_priv(dev);
177 if (cmd == INTERNAL_DEV_SET_STATS) {
178 struct internal_dev_stats stats;
180 if (copy_from_user(&stats, ifr->ifr_data, sizeof(stats)))
183 internal_dev->extra_stats.rx_bytes = stats.rx_bytes;
184 internal_dev->extra_stats.rx_packets = stats.rx_packets;
185 internal_dev->extra_stats.tx_bytes = stats.tx_bytes;
186 internal_dev->extra_stats.tx_packets = stats.tx_packets;
192 return dp_ioctl_hook(dev, ifr, cmd);
196 #ifdef HAVE_NET_DEVICE_OPS
197 static const struct net_device_ops internal_dev_netdev_ops = {
198 .ndo_init = internal_dev_init,
199 .ndo_open = internal_dev_open,
200 .ndo_stop = internal_dev_stop,
201 .ndo_start_xmit = internal_dev_xmit,
202 .ndo_set_mac_address = internal_dev_mac_addr,
203 .ndo_do_ioctl = internal_dev_do_ioctl,
204 .ndo_change_mtu = internal_dev_change_mtu,
205 .ndo_get_stats = internal_dev_get_stats,
210 do_setup(struct net_device *netdev)
214 #ifdef HAVE_NET_DEVICE_OPS
215 netdev->netdev_ops = &internal_dev_netdev_ops;
217 netdev->do_ioctl = internal_dev_do_ioctl;
218 netdev->get_stats = internal_dev_get_stats;
219 netdev->hard_start_xmit = internal_dev_xmit;
220 netdev->open = internal_dev_open;
221 netdev->stop = internal_dev_stop;
222 netdev->set_mac_address = internal_dev_mac_addr;
223 netdev->change_mtu = internal_dev_change_mtu;
224 netdev->init = internal_dev_init;
227 netdev->destructor = internal_dev_free;
228 SET_ETHTOOL_OPS(netdev, &internal_dev_ethtool_ops);
229 netdev->tx_queue_len = 0;
231 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
232 netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_HIGHDMA
233 | NETIF_F_HW_CSUM | NETIF_F_TSO;
235 vport_gen_rand_ether_addr(netdev->dev_addr);
238 static struct vport *
239 internal_dev_create(const char *name, const void __user *config)
242 struct netdev_vport *netdev_vport;
243 struct internal_dev *internal_dev;
246 vport = vport_alloc(sizeof(struct netdev_vport), &internal_vport_ops);
248 err = PTR_ERR(vport);
252 netdev_vport = netdev_vport_priv(vport);
254 netdev_vport->dev = alloc_netdev(sizeof(struct internal_dev), name, do_setup);
255 if (!netdev_vport->dev) {
257 goto error_free_vport;
260 internal_dev = internal_dev_priv(netdev_vport->dev);
261 internal_dev->vport = vport;
263 err = register_netdevice(netdev_vport->dev);
265 goto error_free_netdev;
270 free_netdev(netdev_vport->dev);
278 internal_dev_destroy(struct vport *vport)
280 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
282 unregister_netdevice(netdev_vport->dev);
289 internal_dev_attach(struct vport *vport)
291 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
293 dev_set_promiscuity(netdev_vport->dev, 1);
295 /* It would make sense to assign dev->br_port here too, but
296 * that causes packets received on internal ports to get caught
297 * in netdev_frame_hook(). In turn netdev_frame_hook() can reject them
298 * back to the network stack, but that's a waste of time. */
304 internal_dev_detach(struct vport *vport)
306 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
308 dev_set_promiscuity(netdev_vport->dev, -1);
310 /* Make sure that no packets arrive from now on, since
311 * internal_dev_xmit() will try to find itself through
312 * p->dp->ports[], and we're about to set that to null. */
313 netif_tx_disable(netdev_vport->dev);
319 internal_dev_recv(struct vport *vport, struct sk_buff *skb)
321 struct net_device *netdev = netdev_vport_priv(vport)->dev;
322 struct internal_dev *internal_dev = internal_dev_priv(netdev);
323 struct pcpu_lstats *lb_stats;
328 skb->pkt_type = PACKET_HOST;
329 skb->protocol = eth_type_trans(skb, netdev);
335 netdev->last_rx = jiffies;
338 lb_stats = per_cpu_ptr(internal_dev->lstats, smp_processor_id());
339 lb_stats->rx_packets++;
340 lb_stats->rx_bytes += len;
346 struct vport_ops internal_vport_ops = {
348 .flags = VPORT_F_REQUIRED,
349 .create = internal_dev_create,
350 .destroy = internal_dev_destroy,
351 .attach = internal_dev_attach,
352 .detach = internal_dev_detach,
353 .set_mtu = netdev_set_mtu,
354 .set_addr = netdev_set_addr,
355 .get_name = netdev_get_name,
356 .get_addr = netdev_get_addr,
357 .get_kobj = netdev_get_kobj,
358 .get_stats = netdev_get_stats,
359 .get_dev_flags = netdev_get_dev_flags,
360 .is_running = netdev_is_running,
361 .get_operstate = netdev_get_operstate,
362 .get_ifindex = netdev_get_ifindex,
363 .get_iflink = netdev_get_iflink,
364 .get_mtu = netdev_get_mtu,
365 .send = internal_dev_recv,
368 int is_internal_dev(const struct net_device *netdev)
370 #ifdef HAVE_NET_DEVICE_OPS
371 return netdev->netdev_ops == &internal_dev_netdev_ops;
373 return netdev->open == internal_dev_open;
378 is_internal_vport(const struct vport *vport)
380 return vport->ops == &internal_vport_ops;
384 internal_dev_get_vport(struct net_device *netdev)
386 struct internal_dev *internal_dev = internal_dev_priv(netdev);
387 return rcu_dereference(internal_dev->vport);