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