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/preempt.h>
15 #include <linux/rcupdate.h>
16 #include <linux/skbuff.h>
17 #include <linux/workqueue.h>
20 #include "openvswitch/internal_dev.h"
21 #include "vport-internal_dev.h"
22 #include "vport-netdev.h"
25 unsigned long rx_packets;
26 unsigned long rx_bytes;
27 unsigned long tx_packets;
28 unsigned long tx_bytes;
34 struct net_device_stats stats;
35 struct pcpu_lstats *lstats;
37 /* This is warty support for XAPI, which does not support summing bond
38 * device statistics itself. 'extra_stats' can be set by userspace via
39 * the DP_DEV_SET_STATS ioctl and, if they are, then they are added to
40 * the real device stats. */
41 struct pcpu_lstats extra_stats;
44 struct vport_ops internal_vport_ops;
46 static inline struct internal_dev *internal_dev_priv(struct net_device *netdev)
48 return netdev_priv(netdev);
51 static struct net_device_stats *internal_dev_get_stats(struct net_device *netdev)
53 struct internal_dev *internal_dev = internal_dev_priv(netdev);
54 struct net_device_stats *stats;
57 stats = &internal_dev->stats;
58 stats->rx_bytes = internal_dev->extra_stats.rx_bytes;
59 stats->rx_packets = internal_dev->extra_stats.rx_packets;
60 stats->tx_bytes = internal_dev->extra_stats.tx_bytes;
61 stats->tx_packets = internal_dev->extra_stats.tx_packets;
62 for_each_possible_cpu(i) {
63 const struct pcpu_lstats *lb_stats;
65 lb_stats = per_cpu_ptr(internal_dev->lstats, i);
66 stats->rx_bytes += lb_stats->rx_bytes;
67 stats->rx_packets += lb_stats->rx_packets;
68 stats->tx_bytes += lb_stats->tx_bytes;
69 stats->tx_packets += lb_stats->tx_packets;
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 /* Not reentrant (because it is called with BHs disabled), but may be called
85 * simultaneously on different CPUs. */
86 static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
88 struct internal_dev *internal_dev = internal_dev_priv(netdev);
89 struct vport *vport = internal_dev_get_vport(netdev);
90 struct pcpu_lstats *lb_stats;
92 /* We need our own clone. */
93 skb = skb_share_check(skb, GFP_ATOMIC);
97 lb_stats = per_cpu_ptr(internal_dev->lstats, smp_processor_id());
98 lb_stats->tx_packets++;
99 lb_stats->tx_bytes += skb->len;
101 skb_reset_mac_header(skb);
103 vport_receive(vport, skb);
104 rcu_read_unlock_bh();
109 static int internal_dev_open(struct net_device *netdev)
111 netif_start_queue(netdev);
115 static int internal_dev_stop(struct net_device *netdev)
117 netif_stop_queue(netdev);
121 static void internal_dev_getinfo(struct net_device *netdev,
122 struct ethtool_drvinfo *info)
124 struct dp_port *dp_port = vport_get_dp_port(internal_dev_get_vport(netdev));
126 strcpy(info->driver, "openvswitch");
128 sprintf(info->bus_info, "%d.%d", dp_port->dp->dp_idx, dp_port->port_no);
131 static struct ethtool_ops internal_dev_ethtool_ops = {
132 .get_drvinfo = internal_dev_getinfo,
133 .get_link = ethtool_op_get_link,
134 .get_sg = ethtool_op_get_sg,
135 .get_tx_csum = ethtool_op_get_tx_csum,
136 .get_tso = ethtool_op_get_tso,
139 static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
141 struct dp_port *dp_port = vport_get_dp_port(internal_dev_get_vport(netdev));
149 mutex_lock(&dp_port->dp->mutex);
150 min_mtu = dp_min_mtu(dp_port->dp);
151 mutex_unlock(&dp_port->dp->mutex);
153 if (new_mtu > min_mtu)
157 netdev->mtu = new_mtu;
161 static int internal_dev_init(struct net_device *netdev)
163 struct internal_dev *internal_dev = internal_dev_priv(netdev);
165 internal_dev->lstats = alloc_percpu(struct pcpu_lstats);
166 if (!internal_dev->lstats)
172 static void internal_dev_free(struct net_device *netdev)
174 struct internal_dev *internal_dev = internal_dev_priv(netdev);
176 free_percpu(internal_dev->lstats);
180 static int internal_dev_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
182 struct internal_dev *internal_dev = internal_dev_priv(dev);
184 if (cmd == INTERNAL_DEV_SET_STATS) {
185 struct internal_dev_stats stats;
187 if (copy_from_user(&stats, ifr->ifr_data, sizeof(stats)))
190 internal_dev->extra_stats.rx_bytes = stats.rx_bytes;
191 internal_dev->extra_stats.rx_packets = stats.rx_packets;
192 internal_dev->extra_stats.tx_bytes = stats.tx_bytes;
193 internal_dev->extra_stats.tx_packets = stats.tx_packets;
199 return dp_ioctl_hook(dev, ifr, cmd);
203 #ifdef HAVE_NET_DEVICE_OPS
204 static const struct net_device_ops internal_dev_netdev_ops = {
205 .ndo_init = internal_dev_init,
206 .ndo_open = internal_dev_open,
207 .ndo_stop = internal_dev_stop,
208 .ndo_start_xmit = internal_dev_xmit,
209 .ndo_set_mac_address = internal_dev_mac_addr,
210 .ndo_do_ioctl = internal_dev_do_ioctl,
211 .ndo_change_mtu = internal_dev_change_mtu,
212 .ndo_get_stats = internal_dev_get_stats,
217 do_setup(struct net_device *netdev)
221 #ifdef HAVE_NET_DEVICE_OPS
222 netdev->netdev_ops = &internal_dev_netdev_ops;
224 netdev->do_ioctl = internal_dev_do_ioctl;
225 netdev->get_stats = internal_dev_get_stats;
226 netdev->hard_start_xmit = internal_dev_xmit;
227 netdev->open = internal_dev_open;
228 netdev->stop = internal_dev_stop;
229 netdev->set_mac_address = internal_dev_mac_addr;
230 netdev->change_mtu = internal_dev_change_mtu;
231 netdev->init = internal_dev_init;
234 netdev->destructor = internal_dev_free;
235 SET_ETHTOOL_OPS(netdev, &internal_dev_ethtool_ops);
236 netdev->tx_queue_len = 0;
238 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
239 netdev->features = NETIF_F_LLTX; /* XXX other features? */
241 vport_gen_ether_addr(netdev->dev_addr);
244 static struct vport *
245 internal_dev_create(const char *name, const void __user *config)
248 struct netdev_vport *netdev_vport;
249 struct internal_dev *internal_dev;
252 vport = vport_alloc(sizeof(struct netdev_vport), &internal_vport_ops);
254 err = PTR_ERR(vport);
258 netdev_vport = netdev_vport_priv(vport);
260 netdev_vport->dev = alloc_netdev(sizeof(struct internal_dev), name, do_setup);
261 if (!netdev_vport->dev) {
263 goto error_free_vport;
266 internal_dev = internal_dev_priv(netdev_vport->dev);
267 internal_dev->vport = vport;
269 err = register_netdevice(netdev_vport->dev);
271 goto error_free_netdev;
276 free_netdev(netdev_vport->dev);
284 internal_dev_destroy(struct vport *vport)
286 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
288 unregister_netdevice(netdev_vport->dev);
295 internal_dev_attach(struct vport *vport)
297 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
299 dev_set_promiscuity(netdev_vport->dev, 1);
301 /* It would make sense to assign dev->br_port here too, but
302 * that causes packets received on internal ports to get caught
303 * in netdev_frame_hook(). In turn netdev_frame_hook() can reject them
304 * back to the network stack, but that's a waste of time. */
310 internal_dev_detach(struct vport *vport)
312 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
314 dev_set_promiscuity(netdev_vport->dev, -1);
316 /* Make sure that no packets arrive from now on, since
317 * internal_dev_xmit() will try to find itself through
318 * p->dp->ports[], and we're about to set that to null. */
319 netif_tx_disable(netdev_vport->dev);
325 internal_dev_recv(struct vport *vport, struct sk_buff *skb)
327 struct net_device *netdev = netdev_vport_priv(vport)->dev;
328 struct internal_dev *internal_dev = internal_dev_priv(netdev);
329 struct pcpu_lstats *lb_stats;
334 skb->pkt_type = PACKET_HOST;
335 skb->protocol = eth_type_trans(skb, netdev);
341 netdev->last_rx = jiffies;
344 lb_stats = per_cpu_ptr(internal_dev->lstats, smp_processor_id());
345 lb_stats->rx_packets++;
346 lb_stats->rx_bytes += len;
352 struct vport_ops internal_vport_ops = {
354 .flags = VPORT_F_REQUIRED,
355 .create = internal_dev_create,
356 .destroy = internal_dev_destroy,
357 .attach = internal_dev_attach,
358 .detach = internal_dev_detach,
359 .set_mtu = netdev_set_mtu,
360 .set_addr = netdev_set_addr,
361 .get_name = netdev_get_name,
362 .get_addr = netdev_get_addr,
363 .get_kobj = netdev_get_kobj,
364 .get_stats = netdev_get_stats,
365 .get_dev_flags = netdev_get_dev_flags,
366 .is_running = netdev_is_running,
367 .get_operstate = netdev_get_operstate,
368 .get_ifindex = netdev_get_ifindex,
369 .get_iflink = netdev_get_iflink,
370 .get_mtu = netdev_get_mtu,
371 .send = internal_dev_recv,
374 int is_internal_dev(const struct net_device *netdev)
376 #ifdef HAVE_NET_DEVICE_OPS
377 return netdev->netdev_ops == &internal_dev_netdev_ops;
379 return netdev->open == internal_dev_open;
384 is_internal_vport(const struct vport *vport)
386 return vport->ops == &internal_vport_ops;
390 internal_dev_get_vport(struct net_device *netdev)
392 struct internal_dev *internal_dev = internal_dev_priv(netdev);
393 return rcu_dereference(internal_dev->vport);