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 *skb, struct net_device *netdev)
62 struct dp_dev *dp_dev = dp_dev_priv(netdev);
63 struct datapath *dp = dp_dev->dp;
65 dp_dev->stats.tx_packets++;
66 dp_dev->stats.tx_bytes += skb->len;
68 if (skb_queue_len(&dp_dev->xmit_queue) >= dp->netdev->tx_queue_len) {
69 /* Queue overflow. Stop transmitter. */
70 netif_stop_queue(dp->netdev);
72 /* We won't see all dropped packets individually, so overrun
73 * error is appropriate. */
74 dp_dev->stats.tx_fifo_errors++;
76 skb_queue_tail(&dp_dev->xmit_queue, skb);
77 dp->netdev->trans_start = jiffies;
79 schedule_work(&dp_dev->xmit_work);
84 static void dp_dev_do_xmit(struct work_struct *work)
86 struct dp_dev *dp_dev = container_of(work, struct dp_dev, xmit_work);
87 struct datapath *dp = dp_dev->dp;
90 while ((skb = skb_dequeue(&dp_dev->xmit_queue)) != NULL) {
91 skb_reset_mac_header(skb);
93 fwd_port_input(dp->chain, skb, dp->local_port);
96 netif_wake_queue(dp->netdev);
99 static int dp_dev_open(struct net_device *netdev)
101 netif_start_queue(netdev);
105 static int dp_dev_stop(struct net_device *netdev)
107 netif_stop_queue(netdev);
111 /* Check if the DMI UUID contains a Nicira mac address that should be
112 * used for this interface. The UUID is assumed to be RFC 4122
115 set_uuid_mac(struct net_device *netdev)
117 const char *uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
118 const char *uptr = uuid + 24;
119 uint8_t mac[ETH_ALEN];
122 if (!uuid || *uuid == '\0' || strlen(uuid) != 36)
125 /* We are only interested version 1 UUIDs, since the last six bytes
126 * are an IEEE 802 MAC address. */
130 /* Pull out the embedded MAC address. The kernel's sscanf doesn't
131 * support field widths on hex digits, so we use this hack. */
132 for (i=0; i<ETH_ALEN; i++) {
139 mac[i] = simple_strtoul(d, NULL, 16);
142 /* If this is a Nicira one, then use it. */
143 if (mac[0] != 0x00 || mac[1] != 0x23 || mac[2] != 0x20)
146 memcpy(netdev->dev_addr, mac, ETH_ALEN);
150 do_setup(struct net_device *netdev)
154 netdev->get_stats = dp_dev_get_stats;
155 netdev->hard_start_xmit = dp_dev_xmit;
156 netdev->open = dp_dev_open;
157 netdev->stop = dp_dev_stop;
158 netdev->tx_queue_len = 100;
159 netdev->set_mac_address = dp_dev_mac_addr;
161 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
163 random_ether_addr(netdev->dev_addr);
165 /* Set the OUI to the Nicira one. */
166 netdev->dev_addr[0] = 0x00;
167 netdev->dev_addr[1] = 0x23;
168 netdev->dev_addr[2] = 0x20;
170 /* Set the top bits to indicate random Nicira address. */
171 netdev->dev_addr[3] |= 0xc0;
175 int dp_dev_setup(struct datapath *dp)
177 struct dp_dev *dp_dev;
178 struct net_device *netdev;
182 snprintf(of_name, sizeof of_name, "of%d", dp->dp_idx);
183 netdev = alloc_netdev(sizeof(struct dp_dev), of_name, do_setup);
187 err = register_netdev(netdev);
193 /* For "of0", we check the DMI UUID to see if a Nicira mac address
194 * is available to use instead of the random one just generated. */
196 set_uuid_mac(netdev);
198 dp_dev = dp_dev_priv(netdev);
200 skb_queue_head_init(&dp_dev->xmit_queue);
201 INIT_WORK(&dp_dev->xmit_work, dp_dev_do_xmit);
206 void dp_dev_destroy(struct datapath *dp)
208 struct dp_dev *dp_dev = dp_dev_priv(dp->netdev);
210 netif_tx_disable(dp->netdev);
212 skb_queue_purge(&dp_dev->xmit_queue);
213 unregister_netdev(dp->netdev);
214 free_netdev(dp->netdev);
217 int is_dp_dev(struct net_device *netdev)
219 return netdev->open == dp_dev_open;