2 * Copyright (c) 2009 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/workqueue.h>
21 unsigned long rx_packets;
22 unsigned long rx_bytes;
23 unsigned long tx_packets;
24 unsigned long tx_bytes;
27 struct datapath *dp_dev_get_dp(struct net_device *netdev)
29 return dp_dev_priv(netdev)->dp;
32 static struct net_device_stats *dp_dev_get_stats(struct net_device *netdev)
34 struct dp_dev *dp_dev = dp_dev_priv(netdev);
35 struct net_device_stats *stats;
38 stats = &dp_dev->stats;
39 memset(stats, 0, sizeof *stats);
40 for_each_possible_cpu(i) {
41 const struct pcpu_lstats *lb_stats;
43 lb_stats = per_cpu_ptr(dp_dev->lstats, i);
44 stats->rx_bytes += lb_stats->rx_bytes;
45 stats->rx_packets += lb_stats->rx_packets;
46 stats->tx_bytes += lb_stats->tx_bytes;
47 stats->tx_packets += lb_stats->tx_packets;
52 int dp_dev_recv(struct net_device *netdev, struct sk_buff *skb)
54 struct dp_dev *dp_dev = dp_dev_priv(netdev);
55 struct pcpu_lstats *lb_stats;
58 skb->pkt_type = PACKET_HOST;
59 skb->protocol = eth_type_trans(skb, netdev);
64 netdev->last_rx = jiffies;
65 lb_stats = per_cpu_ptr(dp_dev->lstats, smp_processor_id());
66 lb_stats->rx_packets++;
67 lb_stats->rx_bytes += len;
71 static int dp_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 /* Not reentrant (because it is called with BHs disabled), but may be called
82 * simultaneously on different CPUs. */
83 static int dp_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
85 struct dp_dev *dp_dev = dp_dev_priv(netdev);
86 struct pcpu_lstats *lb_stats;
88 /* dp_process_received_packet() needs its own clone. */
89 skb = skb_share_check(skb, GFP_ATOMIC);
93 lb_stats = per_cpu_ptr(dp_dev->lstats, smp_processor_id());
94 lb_stats->tx_packets++;
95 lb_stats->tx_bytes += skb->len;
97 skb_reset_mac_header(skb);
99 dp_process_received_packet(skb, dp_dev->dp->ports[dp_dev->port_no]);
100 rcu_read_unlock_bh();
105 static int dp_dev_open(struct net_device *netdev)
107 netif_start_queue(netdev);
111 static int dp_dev_stop(struct net_device *netdev)
113 netif_stop_queue(netdev);
117 static void dp_getinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
119 struct dp_dev *dp_dev = dp_dev_priv(netdev);
120 strcpy(info->driver, "openvswitch");
121 sprintf(info->bus_info, "%d", dp_dev->dp->dp_idx);
124 static struct ethtool_ops dp_ethtool_ops = {
125 .get_drvinfo = dp_getinfo,
126 .get_link = ethtool_op_get_link,
127 .get_sg = ethtool_op_get_sg,
128 .get_tx_csum = ethtool_op_get_tx_csum,
129 .get_tso = ethtool_op_get_tso,
132 static int dp_dev_change_mtu(struct net_device *dev, int new_mtu)
134 if (new_mtu < 68 || new_mtu > dp_min_mtu(dp_dev_get_dp(dev)))
141 static int dp_dev_init(struct net_device *netdev)
143 struct dp_dev *dp_dev = dp_dev_priv(netdev);
145 dp_dev->lstats = alloc_percpu(struct pcpu_lstats);
152 static void dp_dev_free(struct net_device *netdev)
154 struct dp_dev *dp_dev = dp_dev_priv(netdev);
156 free_percpu(dp_dev->lstats);
160 static int dp_dev_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
163 return dp_ioctl_hook(dev, ifr, cmd);
167 #ifdef HAVE_NET_DEVICE_OPS
168 static const struct net_device_ops dp_dev_netdev_ops = {
169 .ndo_init = dp_dev_init,
170 .ndo_open = dp_dev_open,
171 .ndo_stop = dp_dev_stop,
172 .ndo_start_xmit = dp_dev_xmit,
173 .ndo_set_mac_address = dp_dev_mac_addr,
174 .ndo_do_ioctl = dp_dev_do_ioctl,
175 .ndo_change_mtu = dp_dev_change_mtu,
176 .ndo_get_stats = dp_dev_get_stats,
181 do_setup(struct net_device *netdev)
185 #ifdef HAVE_NET_DEVICE_OPS
186 netdev->netdev_ops = &dp_dev_netdev_ops;
188 netdev->do_ioctl = dp_dev_do_ioctl;
189 netdev->get_stats = dp_dev_get_stats;
190 netdev->hard_start_xmit = dp_dev_xmit;
191 netdev->open = dp_dev_open;
192 netdev->stop = dp_dev_stop;
193 netdev->set_mac_address = dp_dev_mac_addr;
194 netdev->change_mtu = dp_dev_change_mtu;
195 netdev->init = dp_dev_init;
198 netdev->destructor = dp_dev_free;
199 SET_ETHTOOL_OPS(netdev, &dp_ethtool_ops);
200 netdev->tx_queue_len = 0;
202 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
203 netdev->features = NETIF_F_LLTX; /* XXX other features? */
205 random_ether_addr(netdev->dev_addr);
207 /* Set the OUI to the Nicira one. */
208 netdev->dev_addr[0] = 0x00;
209 netdev->dev_addr[1] = 0x23;
210 netdev->dev_addr[2] = 0x20;
212 /* Set the top bits to indicate random Nicira address. */
213 netdev->dev_addr[3] |= 0xc0;
216 /* Create a datapath device associated with 'dp'. If 'dp_name' is null,
217 * the device name will be of the form 'of<dp_idx>'. Returns the new device or
220 * Called with RTNL lock and dp_mutex. */
221 struct net_device *dp_dev_create(struct datapath *dp, const char *dp_name, int port_no)
223 struct dp_dev *dp_dev;
224 struct net_device *netdev;
225 char dev_name[IFNAMSIZ];
229 if (strlen(dp_name) >= IFNAMSIZ)
230 return ERR_PTR(-EINVAL);
231 strncpy(dev_name, dp_name, sizeof(dev_name));
233 snprintf(dev_name, sizeof dev_name, "of%d", dp->dp_idx);
235 netdev = alloc_netdev(sizeof(struct dp_dev), dev_name, do_setup);
237 return ERR_PTR(-ENOMEM);
239 dp_dev = dp_dev_priv(netdev);
241 dp_dev->port_no = port_no;
242 dp_dev->dev = netdev;
244 err = register_netdevice(netdev);
253 /* Called with RTNL lock and dp_mutex.*/
254 void dp_dev_destroy(struct net_device *netdev)
256 unregister_netdevice(netdev);
259 int is_dp_dev(struct net_device *netdev)
261 #ifdef HAVE_NET_DEVICE_OPS
262 return netdev->netdev_ops == &dp_dev_netdev_ops;
264 return netdev->open == dp_dev_open;