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>
17 #include "vport-generic.h"
18 #include "vport-internal_dev.h"
19 #include "vport-netdev.h"
22 struct vport *attached_vport, *vport;
23 struct net_device_stats stats;
26 static inline struct internal_dev *internal_dev_priv(struct net_device *netdev)
28 return netdev_priv(netdev);
31 /* This function is only called by the kernel network layer. It is not a vport
32 * get_stats() function. If a vport get_stats() function is defined that
33 * results in this being called it will cause infinite recursion. */
34 static struct net_device_stats *internal_dev_sys_stats(struct net_device *netdev)
36 struct vport *vport = internal_dev_get_vport(netdev);
37 struct net_device_stats *stats = &internal_dev_priv(netdev)->stats;
40 struct odp_vport_stats vport_stats;
42 vport_get_stats(vport, &vport_stats);
44 /* The tx and rx stats need to be swapped because the switch
45 * and host OS have opposite perspectives. */
46 stats->rx_packets = vport_stats.tx_packets;
47 stats->tx_packets = vport_stats.rx_packets;
48 stats->rx_bytes = vport_stats.tx_bytes;
49 stats->tx_bytes = vport_stats.rx_bytes;
50 stats->rx_errors = vport_stats.tx_errors;
51 stats->tx_errors = vport_stats.rx_errors;
52 stats->rx_dropped = vport_stats.tx_dropped;
53 stats->tx_dropped = vport_stats.rx_dropped;
54 stats->collisions = vport_stats.collisions;
60 static int internal_dev_mac_addr(struct net_device *dev, void *p)
62 struct sockaddr *addr = p;
64 if (!is_valid_ether_addr(addr->sa_data))
65 return -EADDRNOTAVAIL;
66 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
70 /* Called with rcu_read_lock and bottom-halves disabled. */
71 static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
73 struct internal_dev *internal_dev = internal_dev_priv(netdev);
74 struct vport *vport = rcu_dereference(internal_dev->vport);
76 /* We need our own clone. */
77 skb = skb_share_check(skb, GFP_ATOMIC);
79 vport_record_error(vport, VPORT_E_RX_DROPPED);
83 skb_reset_mac_header(skb);
84 compute_ip_summed(skb, true);
86 vport_receive(vport, skb);
91 static int internal_dev_open(struct net_device *netdev)
93 netif_start_queue(netdev);
97 static int internal_dev_stop(struct net_device *netdev)
99 netif_stop_queue(netdev);
103 static void internal_dev_getinfo(struct net_device *netdev,
104 struct ethtool_drvinfo *info)
106 struct vport *vport = internal_dev_get_vport(netdev);
107 struct dp_port *dp_port;
109 strcpy(info->driver, "openvswitch");
114 dp_port = vport_get_dp_port(vport);
116 sprintf(info->bus_info, "%d.%d", dp_port->dp->dp_idx, dp_port->port_no);
119 static struct ethtool_ops internal_dev_ethtool_ops = {
120 .get_drvinfo = internal_dev_getinfo,
121 .get_link = ethtool_op_get_link,
122 .get_sg = ethtool_op_get_sg,
123 .set_sg = ethtool_op_set_sg,
124 .get_tx_csum = ethtool_op_get_tx_csum,
125 .set_tx_csum = ethtool_op_set_tx_hw_csum,
126 .get_tso = ethtool_op_get_tso,
127 .set_tso = ethtool_op_set_tso,
130 static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
132 struct vport *vport = internal_dev_get_vport(netdev);
138 struct dp_port *dp_port = vport_get_dp_port(vport);
141 if (new_mtu > dp_min_mtu(dp_port->dp))
146 netdev->mtu = new_mtu;
150 static void internal_dev_free(struct net_device *netdev)
155 static int internal_dev_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
158 return dp_ioctl_hook(dev, ifr, cmd);
163 #ifdef HAVE_NET_DEVICE_OPS
164 static const struct net_device_ops internal_dev_netdev_ops = {
165 .ndo_open = internal_dev_open,
166 .ndo_stop = internal_dev_stop,
167 .ndo_start_xmit = internal_dev_xmit,
168 .ndo_set_mac_address = internal_dev_mac_addr,
169 .ndo_do_ioctl = internal_dev_do_ioctl,
170 .ndo_change_mtu = internal_dev_change_mtu,
171 .ndo_get_stats = internal_dev_sys_stats,
175 static void do_setup(struct net_device *netdev)
179 #ifdef HAVE_NET_DEVICE_OPS
180 netdev->netdev_ops = &internal_dev_netdev_ops;
182 netdev->do_ioctl = internal_dev_do_ioctl;
183 netdev->get_stats = internal_dev_sys_stats;
184 netdev->hard_start_xmit = internal_dev_xmit;
185 netdev->open = internal_dev_open;
186 netdev->stop = internal_dev_stop;
187 netdev->set_mac_address = internal_dev_mac_addr;
188 netdev->change_mtu = internal_dev_change_mtu;
191 netdev->destructor = internal_dev_free;
192 SET_ETHTOOL_OPS(netdev, &internal_dev_ethtool_ops);
193 netdev->tx_queue_len = 0;
195 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
196 netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_HIGHDMA
197 | NETIF_F_HW_CSUM | NETIF_F_TSO;
199 vport_gen_rand_ether_addr(netdev->dev_addr);
202 static struct vport *internal_dev_create(const char *name,
203 const void __user *config)
206 struct netdev_vport *netdev_vport;
207 struct internal_dev *internal_dev;
210 vport = vport_alloc(sizeof(struct netdev_vport), &internal_vport_ops);
212 err = PTR_ERR(vport);
216 netdev_vport = netdev_vport_priv(vport);
218 netdev_vport->dev = alloc_netdev(sizeof(struct internal_dev), name, do_setup);
219 if (!netdev_vport->dev) {
221 goto error_free_vport;
224 internal_dev = internal_dev_priv(netdev_vport->dev);
225 rcu_assign_pointer(internal_dev->vport, vport);
227 err = register_netdevice(netdev_vport->dev);
229 goto error_free_netdev;
234 free_netdev(netdev_vport->dev);
241 static int internal_dev_destroy(struct vport *vport)
243 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
245 unregister_netdevice(netdev_vport->dev);
251 static int internal_dev_attach(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 rcu_assign_pointer(internal_dev->attached_vport, internal_dev->vport);
257 dev_set_promiscuity(netdev_vport->dev, 1);
258 netif_start_queue(netdev_vport->dev);
263 static int internal_dev_detach(struct vport *vport)
265 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
266 struct internal_dev *internal_dev = internal_dev_priv(netdev_vport->dev);
268 netif_stop_queue(netdev_vport->dev);
269 dev_set_promiscuity(netdev_vport->dev, -1);
270 rcu_assign_pointer(internal_dev->attached_vport, NULL);
275 static int internal_dev_recv(struct vport *vport, struct sk_buff *skb)
277 struct net_device *netdev = netdev_vport_priv(vport)->dev;
282 skb->pkt_type = PACKET_HOST;
283 skb->protocol = eth_type_trans(skb, netdev);
289 netdev->last_rx = jiffies;
294 struct vport_ops internal_vport_ops = {
296 .flags = VPORT_F_REQUIRED | VPORT_F_GEN_STATS,
297 .create = internal_dev_create,
298 .destroy = internal_dev_destroy,
299 .attach = internal_dev_attach,
300 .detach = internal_dev_detach,
301 .set_mtu = netdev_set_mtu,
302 .set_addr = netdev_set_addr,
303 .get_name = netdev_get_name,
304 .get_addr = netdev_get_addr,
305 .get_kobj = netdev_get_kobj,
306 .get_dev_flags = netdev_get_dev_flags,
307 .is_running = netdev_is_running,
308 .get_operstate = netdev_get_operstate,
309 .get_ifindex = netdev_get_ifindex,
310 .get_iflink = netdev_get_iflink,
311 .get_mtu = netdev_get_mtu,
312 .send = internal_dev_recv,
315 int is_internal_dev(const struct net_device *netdev)
317 #ifdef HAVE_NET_DEVICE_OPS
318 return netdev->netdev_ops == &internal_dev_netdev_ops;
320 return netdev->open == internal_dev_open;
324 int is_internal_vport(const struct vport *vport)
326 return vport->ops == &internal_vport_ops;
329 struct vport *internal_dev_get_vport(struct net_device *netdev)
331 struct internal_dev *internal_dev = internal_dev_priv(netdev);
332 return rcu_dereference(internal_dev->attached_vport);