Stopgap fix for bug #478, where kernel panics on SNAT to input port.
[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 *oskb, struct net_device *netdev)
61 {
62         struct dp_dev *dp_dev = dp_dev_priv(netdev);
63         struct datapath *dp = dp_dev->dp;
64         struct sk_buff *skb;
65
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);
69         skb->dev = oskb->dev;
70         kfree_skb(oskb);
71         if (!skb) {
72                 if (net_ratelimit()) {
73                         printk("failed to copy skb destined for dp_dev\n");
74                 }
75                 return 0;
76         }
77
78         dp_dev->stats.tx_packets++;
79         dp_dev->stats.tx_bytes += skb->len;
80
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);
84
85                 /* We won't see all dropped packets individually, so overrun
86                  * error is appropriate. */
87                 dp_dev->stats.tx_fifo_errors++;
88         }
89         skb_queue_tail(&dp_dev->xmit_queue, skb);
90         dp->netdev->trans_start = jiffies;
91
92         schedule_work(&dp_dev->xmit_work);
93
94         return 0;
95 }
96
97 static void dp_dev_do_xmit(struct work_struct *work)
98 {
99         struct dp_dev *dp_dev = container_of(work, struct dp_dev, xmit_work);
100         struct datapath *dp = dp_dev->dp;
101         struct sk_buff *skb;
102
103         while ((skb = skb_dequeue(&dp_dev->xmit_queue)) != NULL) {
104                 skb_reset_mac_header(skb);
105                 rcu_read_lock();
106                 fwd_port_input(dp->chain, skb, dp->local_port);
107                 rcu_read_unlock();
108         }
109         netif_wake_queue(dp->netdev);
110 }
111
112 static int dp_dev_open(struct net_device *netdev)
113 {
114         netif_start_queue(netdev);
115         return 0;
116 }
117
118 static int dp_dev_stop(struct net_device *netdev)
119 {
120         netif_stop_queue(netdev);
121         return 0;
122 }
123
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
126  * compliant. */
127 static void
128 set_uuid_mac(struct net_device *netdev)
129 {
130         const char *uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
131         const char *uptr = uuid + 24;
132         uint8_t mac[ETH_ALEN];
133         int i;
134
135         if (!uuid || *uuid == '\0' || strlen(uuid) != 36)
136                 return;
137
138         /* We are only interested version 1 UUIDs, since the last six bytes
139          * are an IEEE 802 MAC address. */
140         if (uuid[14] != '1') 
141                 return;
142
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++) {
146                 unsigned char d[3];
147                 
148                 d[0] = *uptr++;
149                 d[1] = *uptr++;
150                 d[2] = '\0';
151                 
152                 mac[i] = simple_strtoul(d, NULL, 16);
153         }
154
155         /* If this is a Nicira one, then use it. */
156         if (mac[0] != 0x00 || mac[1] != 0x23 || mac[2] != 0x20) 
157                 return;
158
159         memcpy(netdev->dev_addr, mac, ETH_ALEN);
160 }
161
162 static void
163 do_setup(struct net_device *netdev)
164 {
165         ether_setup(netdev);
166
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;
173
174         netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
175
176         random_ether_addr(netdev->dev_addr);
177
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;
182
183         /* Set the top bits to indicate random Nicira address. */
184         netdev->dev_addr[3] |= 0xc0;
185 }
186
187
188 int dp_dev_setup(struct datapath *dp)
189 {
190         struct dp_dev *dp_dev;
191         struct net_device *netdev;
192         char of_name[8];
193         int err;
194
195         snprintf(of_name, sizeof of_name, "of%d", dp->dp_idx);
196         netdev = alloc_netdev(sizeof(struct dp_dev), of_name, do_setup);
197         if (!netdev)
198                 return -ENOMEM;
199
200         err = register_netdev(netdev);
201         if (err) {
202                 free_netdev(netdev);
203                 return err;
204         }
205
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. */
208         if (dp->dp_idx == 0) 
209                 set_uuid_mac(netdev);
210
211         dp_dev = dp_dev_priv(netdev);
212         dp_dev->dp = dp;
213         skb_queue_head_init(&dp_dev->xmit_queue);
214         INIT_WORK(&dp_dev->xmit_work, dp_dev_do_xmit);
215         dp->netdev = netdev;
216         return 0;
217 }
218
219 void dp_dev_destroy(struct datapath *dp)
220 {
221         struct dp_dev *dp_dev = dp_dev_priv(dp->netdev);
222
223         netif_tx_disable(dp->netdev);
224         synchronize_net();
225         skb_queue_purge(&dp_dev->xmit_queue);
226         unregister_netdev(dp->netdev);
227         free_netdev(dp->netdev);
228 }
229
230 int is_dp_dev(struct net_device *netdev) 
231 {
232         return netdev->open == dp_dev_open;
233 }