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"
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 vport *vport = internal_dev_get_vport(netdev);
75 /* We need our own clone. */
76 skb = skb_share_check(skb, GFP_ATOMIC);
78 vport_record_error(vport, VPORT_E_RX_DROPPED);
82 skb_reset_mac_header(skb);
83 compute_ip_summed(skb, true);
85 vport_receive(vport, skb);
90 static int internal_dev_open(struct net_device *netdev)
92 netif_start_queue(netdev);
96 static int internal_dev_stop(struct net_device *netdev)
98 netif_stop_queue(netdev);
102 static void internal_dev_getinfo(struct net_device *netdev,
103 struct ethtool_drvinfo *info)
105 struct dp_port *dp_port = vport_get_dp_port(internal_dev_get_vport(netdev));
107 strcpy(info->driver, "openvswitch");
109 sprintf(info->bus_info, "%d.%d", dp_port->dp->dp_idx, dp_port->port_no);
112 static 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 dp_port *dp_port = vport_get_dp_port(internal_dev_get_vport(netdev));
131 if (new_mtu > dp_min_mtu(dp_port->dp))
135 netdev->mtu = new_mtu;
139 static void internal_dev_free(struct net_device *netdev)
144 static int internal_dev_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
147 return dp_ioctl_hook(dev, ifr, cmd);
152 #ifdef HAVE_NET_DEVICE_OPS
153 static const struct net_device_ops internal_dev_netdev_ops = {
154 .ndo_open = internal_dev_open,
155 .ndo_stop = internal_dev_stop,
156 .ndo_start_xmit = internal_dev_xmit,
157 .ndo_set_mac_address = internal_dev_mac_addr,
158 .ndo_do_ioctl = internal_dev_do_ioctl,
159 .ndo_change_mtu = internal_dev_change_mtu,
160 .ndo_get_stats = internal_dev_sys_stats,
164 static void do_setup(struct net_device *netdev)
168 #ifdef HAVE_NET_DEVICE_OPS
169 netdev->netdev_ops = &internal_dev_netdev_ops;
171 netdev->do_ioctl = internal_dev_do_ioctl;
172 netdev->get_stats = internal_dev_sys_stats;
173 netdev->hard_start_xmit = internal_dev_xmit;
174 netdev->open = internal_dev_open;
175 netdev->stop = internal_dev_stop;
176 netdev->set_mac_address = internal_dev_mac_addr;
177 netdev->change_mtu = internal_dev_change_mtu;
180 netdev->destructor = internal_dev_free;
181 SET_ETHTOOL_OPS(netdev, &internal_dev_ethtool_ops);
182 netdev->tx_queue_len = 0;
184 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
185 netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_HIGHDMA
186 | NETIF_F_HW_CSUM | NETIF_F_TSO;
188 vport_gen_rand_ether_addr(netdev->dev_addr);
191 static struct vport *internal_dev_create(const char *name,
192 const void __user *config)
195 struct netdev_vport *netdev_vport;
196 struct internal_dev *internal_dev;
199 vport = vport_alloc(sizeof(struct netdev_vport), &internal_vport_ops);
201 err = PTR_ERR(vport);
205 netdev_vport = netdev_vport_priv(vport);
207 netdev_vport->dev = alloc_netdev(sizeof(struct internal_dev), 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;
223 free_netdev(netdev_vport->dev);
230 static int internal_dev_destroy(struct vport *vport)
232 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
234 unregister_netdevice(netdev_vport->dev);
240 static int internal_dev_attach(struct vport *vport)
242 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
244 dev_set_promiscuity(netdev_vport->dev, 1);
246 /* It would make sense to assign dev->br_port here too, but
247 * that causes packets received on internal ports to get caught
248 * in netdev_frame_hook(). In turn netdev_frame_hook() can reject them
249 * back to the network stack, but that's a waste of time. */
254 static int internal_dev_detach(struct vport *vport)
256 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
258 dev_set_promiscuity(netdev_vport->dev, -1);
260 /* Make sure that no packets arrive from now on, since
261 * internal_dev_xmit() will try to find itself through
262 * p->dp->ports[], and we're about to set that to null. */
263 netif_tx_disable(netdev_vport->dev);
268 static int internal_dev_recv(struct vport *vport, struct sk_buff *skb)
270 struct net_device *netdev = netdev_vport_priv(vport)->dev;
275 skb->pkt_type = PACKET_HOST;
276 skb->protocol = eth_type_trans(skb, netdev);
282 netdev->last_rx = jiffies;
287 struct vport_ops internal_vport_ops = {
289 .flags = VPORT_F_REQUIRED | VPORT_F_GEN_STATS,
290 .create = internal_dev_create,
291 .destroy = internal_dev_destroy,
292 .attach = internal_dev_attach,
293 .detach = internal_dev_detach,
294 .set_mtu = netdev_set_mtu,
295 .set_addr = netdev_set_addr,
296 .get_name = netdev_get_name,
297 .get_addr = netdev_get_addr,
298 .get_kobj = netdev_get_kobj,
299 .get_dev_flags = netdev_get_dev_flags,
300 .is_running = netdev_is_running,
301 .get_operstate = netdev_get_operstate,
302 .get_ifindex = netdev_get_ifindex,
303 .get_iflink = netdev_get_iflink,
304 .get_mtu = netdev_get_mtu,
305 .send = internal_dev_recv,
308 int is_internal_dev(const struct net_device *netdev)
310 #ifdef HAVE_NET_DEVICE_OPS
311 return netdev->netdev_ops == &internal_dev_netdev_ops;
313 return netdev->open == internal_dev_open;
317 int is_internal_vport(const struct vport *vport)
319 return vport->ops == &internal_vport_ops;
322 struct vport *internal_dev_get_vport(struct net_device *netdev)
324 struct internal_dev *internal_dev = internal_dev_priv(netdev);
325 return rcu_dereference(internal_dev->vport);