datapath: Copy Xen's checksumming fields when doing skb_copy.
[openvswitch] / datapath / actions.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008, 2009 Nicira Networks.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 /* Functions for executing flow actions. */
10
11 #include <linux/skbuff.h>
12 #include <linux/in.h>
13 #include <linux/ip.h>
14 #include <linux/tcp.h>
15 #include <linux/udp.h>
16 #include <linux/in6.h>
17 #include <linux/if_vlan.h>
18 #include <net/ip.h>
19 #include <net/checksum.h>
20 #include "datapath.h"
21 #include "dp_dev.h"
22 #include "actions.h"
23 #include "openvswitch/datapath-protocol.h"
24
25 struct sk_buff *
26 make_writable(struct sk_buff *skb, gfp_t gfp)
27 {
28         if (skb_shared(skb) || skb_cloned(skb)) {
29                 struct sk_buff *nskb = skb_copy(skb, gfp);
30                 if (nskb) {
31 #if defined(CONFIG_XEN) && LINUX_VERSION_CODE == KERNEL_VERSION(2,6,18)
32                         /* These fields are copied in skb_clone but not in
33                          * skb_copy or related functions.  We need to manually
34                          * copy them over here. */
35                         nskb->proto_data_valid = skb->proto_data_valid;
36                         nskb->proto_csum_blank = skb->proto_csum_blank;
37 #endif
38                         kfree_skb(skb);
39                         return nskb;
40                 }
41         } else {
42                 unsigned int hdr_len = (skb_transport_offset(skb)
43                                         + sizeof(struct tcphdr));
44                 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
45                         return skb;
46         }
47         kfree_skb(skb);
48         return NULL;
49 }
50
51
52 static struct sk_buff *
53 vlan_pull_tag(struct sk_buff *skb)
54 {
55         struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
56         struct ethhdr *eh;
57
58
59         /* Verify we were given a vlan packet */
60         if (vh->h_vlan_proto != htons(ETH_P_8021Q))
61                 return skb;
62
63         memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
64
65         eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
66
67         skb->protocol = eh->h_proto;
68         skb->mac_header += VLAN_HLEN;
69
70         return skb;
71 }
72
73
74 static struct sk_buff *
75 modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
76                 struct odp_flow_key *key, const union odp_action *a,
77                 int n_actions, gfp_t gfp)
78 {
79         u16 tci, mask;
80
81         if (a->type == ODPAT_SET_VLAN_VID) {
82                 tci = ntohs(a->vlan_vid.vlan_vid);
83                 mask = VLAN_VID_MASK;
84                 key->dl_vlan = htons(tci & mask);
85         } else {
86                 tci = a->vlan_pcp.vlan_pcp << 13;
87                 mask = VLAN_PCP_MASK;
88         }
89
90         skb = make_writable(skb, gfp);
91         if (!skb)
92                 return ERR_PTR(-ENOMEM);
93
94         if (skb->protocol == htons(ETH_P_8021Q)) {
95                 /* Modify vlan id, but maintain other TCI values */
96                 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
97                 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
98         } else {
99                 /* Add vlan header */
100
101                 /* Set up checksumming pointers for checksum-deferred packets
102                  * on Xen.  Otherwise, dev_queue_xmit() will try to do this
103                  * when we send the packet out on the wire, and it will fail at
104                  * that point because skb_checksum_setup() will not look inside
105                  * an 802.1Q header. */
106                 skb_checksum_setup(skb);
107
108                 /* GSO is not implemented for packets with an 802.1Q header, so
109                  * we have to do segmentation before we add that header.
110                  *
111                  * GSO does work with hardware-accelerated VLAN tagging, but we
112                  * can't use hardware-accelerated VLAN tagging since it
113                  * requires the device to have a VLAN group configured (with
114                  * e.g. vconfig(8)) and we don't do that.
115                  *
116                  * Having to do this here may be a performance loss, since we
117                  * can't take advantage of TSO hardware support, although it
118                  * does not make a measurable network performance difference
119                  * for 1G Ethernet.  Fixing that would require patching the
120                  * kernel (either to add GSO support to the VLAN protocol or to
121                  * support hardware-accelerated VLAN tagging without VLAN
122                  * groups configured). */
123                 if (skb_is_gso(skb)) {
124                         struct sk_buff *segs;
125
126                         segs = skb_gso_segment(skb, 0);
127                         kfree_skb(skb);
128                         if (unlikely(IS_ERR(segs)))
129                                 return ERR_CAST(segs);
130
131                         do {
132                                 struct sk_buff *nskb = segs->next;
133                                 int err;
134
135                                 segs->next = NULL;
136
137                                 segs = __vlan_put_tag(segs, tci);
138                                 err = -ENOMEM;
139                                 if (segs) {
140                                         struct odp_flow_key segkey = *key;
141                                         err = execute_actions(dp, segs,
142                                                               &segkey, a + 1,
143                                                               n_actions - 1,
144                                                               gfp);
145                                 }
146
147                                 if (unlikely(err)) {
148                                         while ((segs = nskb)) {
149                                                 nskb = segs->next;
150                                                 segs->next = NULL;
151                                                 kfree_skb(segs);
152                                         }
153                                         return ERR_PTR(err);
154                                 }
155
156                                 segs = nskb;
157                         } while (segs->next);
158
159                         skb = segs;
160                 }
161
162                 /* The hardware-accelerated version of vlan_put_tag() works
163                  * only for a device that has a VLAN group configured (with
164                  * e.g. vconfig(8)), so call the software-only version
165                  * __vlan_put_tag() directly instead.
166                  */
167                 skb = __vlan_put_tag(skb, tci);
168                 if (!skb)
169                         return ERR_PTR(-ENOMEM);
170         }
171
172         return skb;
173 }
174
175 static struct sk_buff *strip_vlan(struct sk_buff *skb,
176                                   struct odp_flow_key *key, gfp_t gfp)
177 {
178         skb = make_writable(skb, gfp);
179         if (skb) {
180                 vlan_pull_tag(skb);
181                 key->dl_vlan = htons(ODP_VLAN_NONE);
182         }
183         return skb;
184 }
185
186 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
187                                    const struct odp_action_dl_addr *a,
188                                    gfp_t gfp)
189 {
190         skb = make_writable(skb, gfp);
191         if (skb) {
192                 struct ethhdr *eh = eth_hdr(skb);
193                 memcpy(a->type == ODPAT_SET_DL_SRC ? eh->h_source : eh->h_dest,
194                        a->dl_addr, ETH_ALEN);
195         }
196         return skb;
197 }
198
199 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
200  * covered by the sum has been changed from 'from' to 'to'.  If set,
201  * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
202  * Based on nf_proto_csum_replace4. */
203 static void update_csum(__sum16 *sum, struct sk_buff *skb,
204                         __be32 from, __be32 to, int pseudohdr)
205 {
206         __be32 diff[] = { ~from, to };
207         if (skb->ip_summed != CHECKSUM_PARTIAL) {
208                 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
209                                 ~csum_unfold(*sum)));
210                 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
211                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
212                                                 ~skb->csum);
213         } else if (pseudohdr)
214                 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
215                                 csum_unfold(*sum)));
216 }
217
218 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
219                                    struct odp_flow_key *key,
220                                    const struct odp_action_nw_addr *a,
221                                    gfp_t gfp)
222 {
223         if (key->dl_type != htons(ETH_P_IP))
224                 return skb;
225
226         skb = make_writable(skb, gfp);
227         if (skb) {
228                 struct iphdr *nh = ip_hdr(skb);
229                 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
230                 u32 old = *f;
231                 u32 new = a->nw_addr;
232
233                 if (key->nw_proto == IPPROTO_TCP) {
234                         struct tcphdr *th = tcp_hdr(skb);
235                         update_csum(&th->check, skb, old, new, 1);
236                 } else if (key->nw_proto == IPPROTO_UDP) {
237                         struct udphdr *th = udp_hdr(skb);
238                         update_csum(&th->check, skb, old, new, 1);
239                 }
240                 update_csum(&nh->check, skb, old, new, 0);
241                 *f = new;
242         }
243         return skb;
244 }
245
246 static struct sk_buff *
247 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
248             const struct odp_action_tp_port *a,
249             gfp_t gfp)
250 {
251         int check_ofs;
252
253         if (key->dl_type != htons(ETH_P_IP))
254                 return skb;
255
256         if (key->nw_proto == IPPROTO_TCP)
257                 check_ofs = offsetof(struct tcphdr, check);
258         else if (key->nw_proto == IPPROTO_UDP)
259                 check_ofs = offsetof(struct udphdr, check);
260         else
261                 return skb;
262
263         skb = make_writable(skb, gfp);
264         if (skb) {
265                 struct udphdr *th = udp_hdr(skb);
266                 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
267                 u16 old = *f;
268                 u16 new = a->tp_port;
269                 update_csum((u16*)((u8*)skb->data + check_ofs),
270                             skb, old, new, 1);
271                 *f = new;
272         }
273         return skb;
274 }
275
276 static inline unsigned packet_length(const struct sk_buff *skb)
277 {
278         unsigned length = skb->len - ETH_HLEN;
279         if (skb->protocol == htons(ETH_P_8021Q))
280                 length -= VLAN_HLEN;
281         return length;
282 }
283
284 int dp_xmit_skb(struct sk_buff *skb)
285 {
286         struct datapath *dp = skb->dev->br_port->dp;
287         int len = skb->len;
288
289         if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) {
290                 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
291                        dp_name(dp), packet_length(skb), skb->dev->mtu);
292                 kfree_skb(skb);
293                 return -E2BIG;
294         }
295
296         dev_queue_xmit(skb);
297
298         return len;
299 }
300
301 static void
302 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
303 {
304         struct net_bridge_port *p;
305         struct net_device *dev;
306
307         if (!skb)
308                 goto error;
309
310         p = dp->ports[out_port];
311         if (!p)
312                 goto error;
313
314         dev = skb->dev = p->dev;
315         if (is_dp_dev(dev))
316                 dp_dev_recv(dev, skb);
317         else
318                 dp_xmit_skb(skb);
319         return;
320
321 error:
322         kfree_skb(skb);
323 }
324
325 /* Never consumes 'skb'.  Returns a port that 'skb' should be sent to, -1 if
326  * none.  */
327 static int output_group(struct datapath *dp, __u16 group,
328                         struct sk_buff *skb, gfp_t gfp)
329 {
330         struct dp_port_group *g = rcu_dereference(dp->groups[group]);
331         int prev_port = -1;
332         int i;
333
334         if (!g)
335                 return -1;
336         for (i = 0; i < g->n_ports; i++) {
337                 struct net_bridge_port *p = dp->ports[g->ports[i]];
338                 if (!p || skb->dev == p->dev)
339                         continue;
340                 if (prev_port != -1) {
341                         struct sk_buff *clone = skb_clone(skb, gfp);
342                         if (!clone)
343                                 return -1;
344                         do_output(dp, clone, prev_port);
345                 }
346                 prev_port = p->port_no;
347         }
348         return prev_port;
349 }
350
351 static int
352 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
353 {
354         skb = skb_clone(skb, gfp);
355         if (!skb)
356                 return -ENOMEM;
357         return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
358 }
359
360 /* Execute a list of actions against 'skb'. */
361 int execute_actions(struct datapath *dp, struct sk_buff *skb,
362                     struct odp_flow_key *key,
363                     const union odp_action *a, int n_actions,
364                     gfp_t gfp)
365 {
366         /* Every output action needs a separate clone of 'skb', but the common
367          * case is just a single output action, so that doing a clone and
368          * then freeing the original skbuff is wasteful.  So the following code
369          * is slightly obscure just to avoid that. */
370         int prev_port = -1;
371         int err = 0;
372         for (; n_actions > 0; a++, n_actions--) {
373                 WARN_ON_ONCE(skb_shared(skb));
374                 if (prev_port != -1) {
375                         do_output(dp, skb_clone(skb, gfp), prev_port);
376                         prev_port = -1;
377                 }
378
379                 switch (a->type) {
380                 case ODPAT_OUTPUT:
381                         prev_port = a->output.port;
382                         break;
383
384                 case ODPAT_OUTPUT_GROUP:
385                         prev_port = output_group(dp, a->output_group.group,
386                                                  skb, gfp);
387                         break;
388
389                 case ODPAT_CONTROLLER:
390                         err = output_control(dp, skb, a->controller.arg, gfp);
391                         if (err) {
392                                 kfree_skb(skb);
393                                 return err;
394                         }
395                         break;
396
397                 case ODPAT_SET_VLAN_VID:
398                 case ODPAT_SET_VLAN_PCP:
399                         skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
400                         if (IS_ERR(skb))
401                                 return PTR_ERR(skb);
402                         break;
403
404                 case ODPAT_STRIP_VLAN:
405                         skb = strip_vlan(skb, key, gfp);
406                         break;
407
408                 case ODPAT_SET_DL_SRC:
409                 case ODPAT_SET_DL_DST:
410                         skb = set_dl_addr(skb, &a->dl_addr, gfp);
411                         break;
412
413                 case ODPAT_SET_NW_SRC:
414                 case ODPAT_SET_NW_DST:
415                         skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
416                         break;
417
418                 case ODPAT_SET_TP_SRC:
419                 case ODPAT_SET_TP_DST:
420                         skb = set_tp_port(skb, key, &a->tp_port, gfp);
421                         break;
422                 }
423                 if (!skb)
424                         return -ENOMEM;
425         }
426         if (prev_port != -1)
427                 do_output(dp, skb, prev_port);
428         else
429                 kfree_skb(skb);
430         return err;
431 }