ee66638b520c972295fa67566988b3d43bb37b90
[sliver-openvswitch.git] / datapath / dp_dev.c
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>
7 #include <linux/dmi.h>
8
9 #include "datapath.h"
10 #include "forward.h"
11
12 struct dp_dev {
13         struct net_device_stats stats;
14         struct datapath *dp;
15         struct sk_buff_head xmit_queue;
16         struct work_struct xmit_work;
17 };
18
19
20 static struct dp_dev *dp_dev_priv(struct net_device *netdev) 
21 {
22         return netdev_priv(netdev);
23 }
24
25 static struct net_device_stats *dp_dev_get_stats(struct net_device *netdev)
26 {
27         struct dp_dev *dp_dev = dp_dev_priv(netdev);
28         return &dp_dev->stats;
29 }
30
31 int dp_dev_recv(struct net_device *netdev, struct sk_buff *skb) 
32 {
33         int len = skb->len;
34         struct dp_dev *dp_dev = dp_dev_priv(netdev);
35         skb->dev = netdev;
36         skb->pkt_type = PACKET_HOST;
37         skb->protocol = eth_type_trans(skb, netdev);
38         if (in_interrupt())
39                 netif_rx(skb);
40         else
41                 netif_rx_ni(skb);
42         netdev->last_rx = jiffies;
43         dp_dev->stats.rx_packets++;
44         dp_dev->stats.rx_bytes += len;
45         return len;
46 }
47
48 static int dp_dev_mac_addr(struct net_device *dev, void *p)
49 {
50         struct sockaddr *addr = p;
51
52         if (netif_running(dev))
53                 return -EBUSY;
54         if (!is_valid_ether_addr(addr->sa_data))
55                 return -EADDRNOTAVAIL;
56         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
57         return 0;
58 }
59
60 static int dp_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
61 {
62         struct dp_dev *dp_dev = dp_dev_priv(netdev);
63         struct datapath *dp = dp_dev->dp;
64
65         dp_dev->stats.tx_packets++;
66         dp_dev->stats.tx_bytes += skb->len;
67
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);
71
72                 /* We won't see all dropped packets individually, so overrun
73                  * error is appropriate. */
74                 dp_dev->stats.tx_fifo_errors++;
75         }
76         skb_queue_tail(&dp_dev->xmit_queue, skb);
77         dp->netdev->trans_start = jiffies;
78
79         schedule_work(&dp_dev->xmit_work);
80
81         return 0;
82 }
83
84 static void dp_dev_do_xmit(struct work_struct *work)
85 {
86         struct dp_dev *dp_dev = container_of(work, struct dp_dev, xmit_work);
87         struct datapath *dp = dp_dev->dp;
88         struct sk_buff *skb;
89
90         while ((skb = skb_dequeue(&dp_dev->xmit_queue)) != NULL) {
91                 skb_reset_mac_header(skb);
92                 rcu_read_lock();
93                 fwd_port_input(dp->chain, skb, dp->local_port);
94                 rcu_read_unlock();
95         }
96         netif_wake_queue(dp->netdev);
97 }
98
99 static int dp_dev_open(struct net_device *netdev)
100 {
101         netif_start_queue(netdev);
102         return 0;
103 }
104
105 static int dp_dev_stop(struct net_device *netdev)
106 {
107         netif_stop_queue(netdev);
108         return 0;
109 }
110
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
113  * compliant. */
114 static void
115 set_uuid_mac(struct net_device *netdev)
116 {
117         const char *uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
118         const char *uptr = uuid + 24;
119         uint8_t mac[ETH_ALEN];
120         int i;
121
122         if (!uuid || *uuid == '\0' || strlen(uuid) != 36)
123                 return;
124
125         /* We are only interested version 1 UUIDs, since the last six bytes
126          * are an IEEE 802 MAC address. */
127         if (uuid[14] != '1') 
128                 return;
129
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++) {
133                 unsigned char d[3];
134                 
135                 d[0] = *uptr++;
136                 d[1] = *uptr++;
137                 d[2] = '\0';
138                 
139                 mac[i] = simple_strtoul(d, NULL, 16);
140         }
141
142         /* If this is a Nicira one, then use it. */
143         if (mac[0] != 0x00 || mac[1] != 0x23 || mac[2] != 0x20) 
144                 return;
145
146         memcpy(netdev->dev_addr, mac, ETH_ALEN);
147 }
148
149 static void
150 do_setup(struct net_device *netdev)
151 {
152         ether_setup(netdev);
153
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;
160
161         netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
162
163         random_ether_addr(netdev->dev_addr);
164
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;
169
170         /* Set the top bits to indicate random Nicira address. */
171         netdev->dev_addr[3] |= 0xc0;
172 }
173
174
175 int dp_dev_setup(struct datapath *dp)
176 {
177         struct dp_dev *dp_dev;
178         struct net_device *netdev;
179         char of_name[8];
180         int err;
181
182         snprintf(of_name, sizeof of_name, "of%d", dp->dp_idx);
183         netdev = alloc_netdev(sizeof(struct dp_dev), of_name, do_setup);
184         if (!netdev)
185                 return -ENOMEM;
186
187         err = register_netdev(netdev);
188         if (err) {
189                 free_netdev(netdev);
190                 return err;
191         }
192
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. */
195         if (dp->dp_idx == 0) 
196                 set_uuid_mac(netdev);
197
198         dp_dev = dp_dev_priv(netdev);
199         dp_dev->dp = dp;
200         skb_queue_head_init(&dp_dev->xmit_queue);
201         INIT_WORK(&dp_dev->xmit_work, dp_dev_do_xmit);
202         dp->netdev = netdev;
203         return 0;
204 }
205
206 void dp_dev_destroy(struct datapath *dp)
207 {
208         struct dp_dev *dp_dev = dp_dev_priv(dp->netdev);
209
210         netif_tx_disable(dp->netdev);
211         synchronize_net();
212         skb_queue_purge(&dp_dev->xmit_queue);
213         unregister_netdev(dp->netdev);
214         free_netdev(dp->netdev);
215 }
216
217 int is_dp_dev(struct net_device *netdev) 
218 {
219         return netdev->open == dp_dev_open;
220 }