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>
20 struct datapath *dp_dev_get_dp(struct net_device *netdev)
22 return dp_dev_priv(netdev)->dp;
24 EXPORT_SYMBOL(dp_dev_get_dp);
26 static struct net_device_stats *dp_dev_get_stats(struct net_device *netdev)
28 struct dp_dev *dp_dev = dp_dev_priv(netdev);
29 return &dp_dev->stats;
32 int dp_dev_recv(struct net_device *netdev, struct sk_buff *skb)
34 struct dp_dev *dp_dev = dp_dev_priv(netdev);
37 skb->pkt_type = PACKET_HOST;
38 skb->protocol = eth_type_trans(skb, netdev);
43 netdev->last_rx = jiffies;
44 dp_dev->stats.rx_packets++;
45 dp_dev->stats.rx_bytes += len;
49 static int dp_dev_mac_addr(struct net_device *dev, void *p)
51 struct sockaddr *addr = p;
53 if (!is_valid_ether_addr(addr->sa_data))
54 return -EADDRNOTAVAIL;
55 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
59 static int dp_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
61 struct dp_dev *dp_dev = dp_dev_priv(netdev);
63 /* By orphaning 'skb' we will screw up socket accounting slightly, but
64 * the effect is limited to the device queue length. If we don't
65 * do this, then the sk_buff will be destructed eventually, but it is
66 * harder to predict when. */
69 /* We are going to modify 'skb', by sticking it on &dp_dev->xmit_queue,
70 * so we need to have our own clone. (At any rate, fwd_port_input()
71 * will need its own clone, so there's no benefit to queuing any other
73 skb = skb_share_check(skb, GFP_ATOMIC);
77 dp_dev->stats.tx_packets++;
78 dp_dev->stats.tx_bytes += skb->len;
80 if (skb_queue_len(&dp_dev->xmit_queue) >= netdev->tx_queue_len) {
81 /* Queue overflow. Stop transmitter. */
82 netif_stop_queue(netdev);
84 /* We won't see all dropped packets individually, so overrun
85 * error is appropriate. */
86 dp_dev->stats.tx_fifo_errors++;
88 skb_queue_tail(&dp_dev->xmit_queue, skb);
89 netdev->trans_start = jiffies;
91 schedule_work(&dp_dev->xmit_work);
96 static void dp_dev_do_xmit(struct work_struct *work)
98 struct dp_dev *dp_dev = container_of(work, struct dp_dev, xmit_work);
99 struct datapath *dp = dp_dev->dp;
102 while ((skb = skb_dequeue(&dp_dev->xmit_queue)) != NULL) {
103 skb_reset_mac_header(skb);
105 dp_process_received_packet(skb, dp->ports[dp_dev->port_no]);
106 rcu_read_unlock_bh();
108 netif_wake_queue(dp_dev->dev);
111 static int dp_dev_open(struct net_device *netdev)
113 netif_start_queue(netdev);
117 static int dp_dev_stop(struct net_device *netdev)
119 netif_stop_queue(netdev);
123 static void dp_getinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
125 struct dp_dev *dp_dev = dp_dev_priv(netdev);
126 strcpy(info->driver, "openvswitch");
127 sprintf(info->bus_info, "%d.%d", dp_dev->dp->dp_idx, dp_dev->port_no);
130 static struct ethtool_ops dp_ethtool_ops = {
131 .get_drvinfo = dp_getinfo,
132 .get_link = ethtool_op_get_link,
133 .get_sg = ethtool_op_get_sg,
134 .get_tx_csum = ethtool_op_get_tx_csum,
135 .get_tso = ethtool_op_get_tso,
139 do_setup(struct net_device *netdev)
143 netdev->do_ioctl = dp_ioctl_hook;
144 netdev->get_stats = dp_dev_get_stats;
145 netdev->hard_start_xmit = dp_dev_xmit;
146 netdev->open = dp_dev_open;
147 SET_ETHTOOL_OPS(netdev, &dp_ethtool_ops);
148 netdev->stop = dp_dev_stop;
149 netdev->tx_queue_len = 100;
150 netdev->set_mac_address = dp_dev_mac_addr;
152 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
154 random_ether_addr(netdev->dev_addr);
156 /* Set the OUI to the Nicira one. */
157 netdev->dev_addr[0] = 0x00;
158 netdev->dev_addr[1] = 0x23;
159 netdev->dev_addr[2] = 0x20;
161 /* Set the top bits to indicate random Nicira address. */
162 netdev->dev_addr[3] |= 0xc0;
165 /* Create a datapath device associated with 'dp'. If 'dp_name' is null,
166 * the device name will be of the form 'of<dp_idx>'. Returns the new device or
169 * Called with RTNL lock and dp_mutex. */
170 struct net_device *dp_dev_create(struct datapath *dp, const char *dp_name, int port_no)
172 struct dp_dev *dp_dev;
173 struct net_device *netdev;
174 char dev_name[IFNAMSIZ];
178 if (strlen(dp_name) >= IFNAMSIZ)
179 return ERR_PTR(-EINVAL);
180 strncpy(dev_name, dp_name, sizeof(dev_name));
182 snprintf(dev_name, sizeof dev_name, "of%d", dp->dp_idx);
184 netdev = alloc_netdev(sizeof(struct dp_dev), dev_name, do_setup);
186 return ERR_PTR(-ENOMEM);
188 err = register_netdevice(netdev);
194 dp_dev = dp_dev_priv(netdev);
196 dp_dev->port_no = port_no;
197 dp_dev->dev = netdev;
198 skb_queue_head_init(&dp_dev->xmit_queue);
199 INIT_WORK(&dp_dev->xmit_work, dp_dev_do_xmit);
203 /* Called with RTNL lock and dp_mutex.*/
204 void dp_dev_destroy(struct net_device *netdev)
206 struct dp_dev *dp_dev = dp_dev_priv(netdev);
208 netif_tx_disable(netdev);
210 skb_queue_purge(&dp_dev->xmit_queue);
211 unregister_netdevice(netdev);
214 int is_dp_dev(struct net_device *netdev)
216 return netdev->open == dp_dev_open;
218 EXPORT_SYMBOL(is_dp_dev);