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 /* By orphaning 'skb' we will screw up socket accounting slightly, but
66 * the effect is limited to the device queue length. If we don't
67 * do this, then the sk_buff will be destructed eventually, but it is
68 * harder to predict when. */
71 /* We are going to modify 'skb', by sticking it on &dp_dev->xmit_queue,
72 * so we need to have our own clone. (At any rate, fwd_port_input()
73 * will need its own clone, so there's no benefit to queuing any other
75 skb = skb_share_check(skb, GFP_ATOMIC);
79 dp_dev->stats.tx_packets++;
80 dp_dev->stats.tx_bytes += skb->len;
82 if (skb_queue_len(&dp_dev->xmit_queue) >= dp->netdev->tx_queue_len) {
83 /* Queue overflow. Stop transmitter. */
84 netif_stop_queue(dp->netdev);
86 /* We won't see all dropped packets individually, so overrun
87 * error is appropriate. */
88 dp_dev->stats.tx_fifo_errors++;
90 skb_queue_tail(&dp_dev->xmit_queue, skb);
91 dp->netdev->trans_start = jiffies;
93 schedule_work(&dp_dev->xmit_work);
98 static void dp_dev_do_xmit(struct work_struct *work)
100 struct dp_dev *dp_dev = container_of(work, struct dp_dev, xmit_work);
101 struct datapath *dp = dp_dev->dp;
104 while ((skb = skb_dequeue(&dp_dev->xmit_queue)) != NULL) {
105 skb_reset_mac_header(skb);
107 fwd_port_input(dp->chain, skb, dp->local_port);
110 netif_wake_queue(dp->netdev);
113 static int dp_dev_open(struct net_device *netdev)
115 netif_start_queue(netdev);
119 static int dp_dev_stop(struct net_device *netdev)
121 netif_stop_queue(netdev);
125 /* Check if the DMI UUID contains a Nicira mac address that should be
126 * used for this interface. The UUID is assumed to be RFC 4122
129 set_uuid_mac(struct net_device *netdev)
131 const char *uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
133 uint8_t mac[ETH_ALEN];
136 if (!uuid || *uuid == '\0' || strlen(uuid) != 36)
139 /* We are only interested version 1 UUIDs, since the last six bytes
140 * are an IEEE 802 MAC address. */
144 /* Pull out the embedded MAC address. The kernel's sscanf doesn't
145 * support field widths on hex digits, so we use this hack. */
147 for (i=0; i<ETH_ALEN; i++) {
154 mac[i] = simple_strtoul(d, NULL, 16);
157 /* If this is a Nicira one, then use it. */
158 if (mac[0] != 0x00 || mac[1] != 0x23 || mac[2] != 0x20)
161 memcpy(netdev->dev_addr, mac, ETH_ALEN);
165 do_setup(struct net_device *netdev)
169 netdev->get_stats = dp_dev_get_stats;
170 netdev->hard_start_xmit = dp_dev_xmit;
171 netdev->open = dp_dev_open;
172 netdev->stop = dp_dev_stop;
173 netdev->tx_queue_len = 100;
174 netdev->set_mac_address = dp_dev_mac_addr;
176 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
178 random_ether_addr(netdev->dev_addr);
180 /* Set the OUI to the Nicira one. */
181 netdev->dev_addr[0] = 0x00;
182 netdev->dev_addr[1] = 0x23;
183 netdev->dev_addr[2] = 0x20;
185 /* Set the top bits to indicate random Nicira address. */
186 netdev->dev_addr[3] |= 0xc0;
190 int dp_dev_setup(struct datapath *dp)
192 struct dp_dev *dp_dev;
193 struct net_device *netdev;
197 snprintf(of_name, sizeof of_name, "of%d", dp->dp_idx);
198 netdev = alloc_netdev(sizeof(struct dp_dev), of_name, do_setup);
202 err = register_netdev(netdev);
208 /* For "of0", we check the DMI UUID to see if a Nicira mac address
209 * is available to use instead of the random one just generated. */
211 set_uuid_mac(netdev);
213 dp_dev = dp_dev_priv(netdev);
215 skb_queue_head_init(&dp_dev->xmit_queue);
216 INIT_WORK(&dp_dev->xmit_work, dp_dev_do_xmit);
221 void dp_dev_destroy(struct datapath *dp)
223 struct dp_dev *dp_dev = dp_dev_priv(dp->netdev);
225 netif_tx_disable(dp->netdev);
227 skb_queue_purge(&dp_dev->xmit_queue);
228 unregister_netdev(dp->netdev);
229 free_netdev(dp->netdev);
232 int is_dp_dev(struct net_device *netdev)
234 return netdev->open == dp_dev_open;