1 #include <linux/kernel.h>
2 #include <linux/netdevice.h>
3 #include <linux/etherdevice.h>
4 #include <linux/rcupdate.h>
5 #include <linux/skbuff.h>
6 #include <linux/workqueue.h>
13 struct net_device_stats stats;
15 struct sk_buff_head xmit_queue;
16 struct work_struct xmit_work;
20 static struct dp_dev *dp_dev_priv(struct net_device *netdev)
22 return netdev_priv(netdev);
25 static struct net_device_stats *dp_dev_get_stats(struct net_device *netdev)
27 struct dp_dev *dp_dev = dp_dev_priv(netdev);
28 return &dp_dev->stats;
31 int dp_dev_recv(struct net_device *netdev, struct sk_buff *skb)
34 struct dp_dev *dp_dev = dp_dev_priv(netdev);
36 skb->pkt_type = PACKET_HOST;
37 skb->protocol = eth_type_trans(skb, netdev);
42 netdev->last_rx = jiffies;
43 dp_dev->stats.rx_packets++;
44 dp_dev->stats.rx_bytes += len;
48 static int dp_dev_mac_addr(struct net_device *dev, void *p)
50 struct sockaddr *addr = p;
52 if (netif_running(dev))
54 if (!is_valid_ether_addr(addr->sa_data))
55 return -EADDRNOTAVAIL;
56 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
60 static int dp_dev_xmit(struct sk_buff *oskb, struct net_device *netdev)
62 struct dp_dev *dp_dev = dp_dev_priv(netdev);
63 struct datapath *dp = dp_dev->dp;
66 /* FIXME: doing a full copy here is far too expensive and most the time
67 * it is unnecessary. However, it is a stopgap fix for bug #478. */
68 skb = skb_copy(oskb, GFP_ATOMIC);
72 if (net_ratelimit()) {
73 printk("failed to copy skb destined for dp_dev\n");
78 dp_dev->stats.tx_packets++;
79 dp_dev->stats.tx_bytes += skb->len;
81 if (skb_queue_len(&dp_dev->xmit_queue) >= dp->netdev->tx_queue_len) {
82 /* Queue overflow. Stop transmitter. */
83 netif_stop_queue(dp->netdev);
85 /* We won't see all dropped packets individually, so overrun
86 * error is appropriate. */
87 dp_dev->stats.tx_fifo_errors++;
89 skb_queue_tail(&dp_dev->xmit_queue, skb);
90 dp->netdev->trans_start = jiffies;
92 schedule_work(&dp_dev->xmit_work);
97 static void dp_dev_do_xmit(struct work_struct *work)
99 struct dp_dev *dp_dev = container_of(work, struct dp_dev, xmit_work);
100 struct datapath *dp = dp_dev->dp;
103 while ((skb = skb_dequeue(&dp_dev->xmit_queue)) != NULL) {
104 skb_reset_mac_header(skb);
106 fwd_port_input(dp->chain, skb, dp->local_port);
109 netif_wake_queue(dp->netdev);
112 static int dp_dev_open(struct net_device *netdev)
114 netif_start_queue(netdev);
118 static int dp_dev_stop(struct net_device *netdev)
120 netif_stop_queue(netdev);
124 /* Check if the DMI UUID contains a Nicira mac address that should be
125 * used for this interface. The UUID is assumed to be RFC 4122
128 set_uuid_mac(struct net_device *netdev)
130 const char *uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
131 const char *uptr = uuid + 24;
132 uint8_t mac[ETH_ALEN];
135 if (!uuid || *uuid == '\0' || strlen(uuid) != 36)
138 /* We are only interested version 1 UUIDs, since the last six bytes
139 * are an IEEE 802 MAC address. */
143 /* Pull out the embedded MAC address. The kernel's sscanf doesn't
144 * support field widths on hex digits, so we use this hack. */
145 for (i=0; i<ETH_ALEN; i++) {
152 mac[i] = simple_strtoul(d, NULL, 16);
155 /* If this is a Nicira one, then use it. */
156 if (mac[0] != 0x00 || mac[1] != 0x23 || mac[2] != 0x20)
159 memcpy(netdev->dev_addr, mac, ETH_ALEN);
163 do_setup(struct net_device *netdev)
167 netdev->get_stats = dp_dev_get_stats;
168 netdev->hard_start_xmit = dp_dev_xmit;
169 netdev->open = dp_dev_open;
170 netdev->stop = dp_dev_stop;
171 netdev->tx_queue_len = 100;
172 netdev->set_mac_address = dp_dev_mac_addr;
174 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
176 random_ether_addr(netdev->dev_addr);
178 /* Set the OUI to the Nicira one. */
179 netdev->dev_addr[0] = 0x00;
180 netdev->dev_addr[1] = 0x23;
181 netdev->dev_addr[2] = 0x20;
183 /* Set the top bits to indicate random Nicira address. */
184 netdev->dev_addr[3] |= 0xc0;
188 int dp_dev_setup(struct datapath *dp)
190 struct dp_dev *dp_dev;
191 struct net_device *netdev;
195 snprintf(of_name, sizeof of_name, "of%d", dp->dp_idx);
196 netdev = alloc_netdev(sizeof(struct dp_dev), of_name, do_setup);
200 err = register_netdev(netdev);
206 /* For "of0", we check the DMI UUID to see if a Nicira mac address
207 * is available to use instead of the random one just generated. */
209 set_uuid_mac(netdev);
211 dp_dev = dp_dev_priv(netdev);
213 skb_queue_head_init(&dp_dev->xmit_queue);
214 INIT_WORK(&dp_dev->xmit_work, dp_dev_do_xmit);
219 void dp_dev_destroy(struct datapath *dp)
221 struct dp_dev *dp_dev = dp_dev_priv(dp->netdev);
223 netif_tx_disable(dp->netdev);
225 skb_queue_purge(&dp_dev->xmit_queue);
226 unregister_netdev(dp->netdev);
227 free_netdev(dp->netdev);
230 int is_dp_dev(struct net_device *netdev)
232 return netdev->open == dp_dev_open;