2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009, 2010 Nicira Networks.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 /* Functions for executing flow actions. */
11 #include <linux/skbuff.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/inet_ecn.h>
20 #include <net/checksum.h>
24 #include "openvswitch/datapath-protocol.h"
27 static struct sk_buff *make_writable(struct sk_buff *skb, unsigned min_headroom, gfp_t gfp)
29 if (skb_shared(skb) || skb_cloned(skb)) {
31 unsigned headroom = max(min_headroom, skb_headroom(skb));
33 nskb = skb_copy_expand(skb, headroom, skb_tailroom(skb), gfp);
35 set_skb_csum_bits(skb, nskb);
40 unsigned int hdr_len = (skb_transport_offset(skb)
41 + sizeof(struct tcphdr));
42 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
49 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
51 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
54 /* Verify we were given a vlan packet */
55 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
58 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
59 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
60 + ETH_HLEN, VLAN_HLEN, 0));
62 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
64 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
66 skb->protocol = eh->h_proto;
67 skb->mac_header += VLAN_HLEN;
72 static struct sk_buff *modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
73 struct odp_flow_key *key, const union odp_action *a,
74 int n_actions, gfp_t gfp)
78 if (a->type == ODPAT_SET_VLAN_VID) {
79 tci = ntohs(a->vlan_vid.vlan_vid);
81 key->dl_vlan = a->vlan_vid.vlan_vid;
83 tci = a->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT;
85 key->dl_vlan_pcp = a->vlan_pcp.vlan_pcp;
88 skb = make_writable(skb, VLAN_HLEN, gfp);
90 return ERR_PTR(-ENOMEM);
92 if (skb->protocol == htons(ETH_P_8021Q)) {
93 /* Modify vlan id, but maintain other TCI values */
94 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
95 __be16 old_tci = vh->h_vlan_TCI;
97 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
99 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE) {
100 __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
102 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
108 /* Add vlan header */
110 /* Set up checksumming pointers for checksum-deferred packets
111 * on Xen. Otherwise, dev_queue_xmit() will try to do this
112 * when we send the packet out on the wire, and it will fail at
113 * that point because skb_checksum_setup() will not look inside
114 * an 802.1Q header. */
115 err = vswitch_skb_checksum_setup(skb);
121 /* GSO is not implemented for packets with an 802.1Q header, so
122 * we have to do segmentation before we add that header.
124 * GSO does work with hardware-accelerated VLAN tagging, but we
125 * can't use hardware-accelerated VLAN tagging since it
126 * requires the device to have a VLAN group configured (with
127 * e.g. vconfig(8)) and we don't do that.
129 * Having to do this here may be a performance loss, since we
130 * can't take advantage of TSO hardware support, although it
131 * does not make a measurable network performance difference
132 * for 1G Ethernet. Fixing that would require patching the
133 * kernel (either to add GSO support to the VLAN protocol or to
134 * support hardware-accelerated VLAN tagging without VLAN
135 * groups configured). */
136 if (skb_is_gso(skb)) {
137 struct sk_buff *segs;
139 segs = skb_gso_segment(skb, 0);
141 if (unlikely(IS_ERR(segs)))
142 return ERR_CAST(segs);
145 struct sk_buff *nskb = segs->next;
150 /* GSO can change the checksum type so update.*/
151 compute_ip_summed(segs, true);
153 segs = __vlan_put_tag(segs, tci);
156 struct odp_flow_key segkey = *key;
157 err = execute_actions(dp, segs,
164 while ((segs = nskb)) {
173 } while (segs->next);
176 compute_ip_summed(skb, true);
179 /* The hardware-accelerated version of vlan_put_tag() works
180 * only for a device that has a VLAN group configured (with
181 * e.g. vconfig(8)), so call the software-only version
182 * __vlan_put_tag() directly instead.
184 skb = __vlan_put_tag(skb, tci);
186 return ERR_PTR(-ENOMEM);
188 /* GSO doesn't fix up the hardware computed checksum so this
189 * will only be hit in the non-GSO case. */
190 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
191 skb->csum = csum_add(skb->csum, csum_partial(skb->data
192 + ETH_HLEN, VLAN_HLEN, 0));
198 static struct sk_buff *strip_vlan(struct sk_buff *skb,
199 struct odp_flow_key *key, gfp_t gfp)
201 skb = make_writable(skb, 0, gfp);
204 key->dl_vlan = htons(ODP_VLAN_NONE);
209 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
210 struct odp_flow_key *key,
211 const struct odp_action_dl_addr *a,
214 skb = make_writable(skb, 0, gfp);
216 struct ethhdr *eh = eth_hdr(skb);
217 if (a->type == ODPAT_SET_DL_SRC) {
218 memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
219 memcpy(key->dl_src, a->dl_addr, ETH_ALEN);
221 memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
222 memcpy(key->dl_dst, a->dl_addr, ETH_ALEN);
228 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
229 * covered by the sum has been changed from 'from' to 'to'. If set,
230 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
231 * Based on nf_proto_csum_replace4. */
232 static void update_csum(__sum16 *sum, struct sk_buff *skb,
233 __be32 from, __be32 to, int pseudohdr)
235 __be32 diff[] = { ~from, to };
237 if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
238 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
239 ~csum_unfold(*sum)));
240 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
241 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
243 } else if (pseudohdr)
244 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
248 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
249 struct odp_flow_key *key,
250 const struct odp_action_nw_addr *a,
253 if (key->dl_type != htons(ETH_P_IP))
256 skb = make_writable(skb, 0, gfp);
258 struct iphdr *nh = ip_hdr(skb);
259 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
261 u32 new = a->nw_addr;
263 if (key->nw_proto == IPPROTO_TCP) {
264 struct tcphdr *th = tcp_hdr(skb);
265 update_csum(&th->check, skb, old, new, 1);
266 } else if (key->nw_proto == IPPROTO_UDP) {
267 struct udphdr *th = udp_hdr(skb);
268 update_csum(&th->check, skb, old, new, 1);
270 update_csum(&nh->check, skb, old, new, 0);
273 if (a->type == ODPAT_SET_NW_SRC)
274 key->nw_src = a->nw_addr;
276 key->nw_dst = a->nw_addr;
281 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
282 struct odp_flow_key *key,
283 const struct odp_action_nw_tos *a,
286 if (key->dl_type != htons(ETH_P_IP))
289 skb = make_writable(skb, 0, gfp);
291 struct iphdr *nh = ip_hdr(skb);
296 /* Set the DSCP bits and preserve the ECN bits. */
297 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
298 update_csum(&nh->check, skb, htons((uint16_t)old),
299 htons((uint16_t)new), 0);
301 key->nw_tos = a->nw_tos;
306 static struct sk_buff *set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
307 const struct odp_action_tp_port *a, gfp_t gfp)
311 if (key->dl_type != htons(ETH_P_IP))
314 if (key->nw_proto == IPPROTO_TCP)
315 check_ofs = offsetof(struct tcphdr, check);
316 else if (key->nw_proto == IPPROTO_UDP)
317 check_ofs = offsetof(struct udphdr, check);
321 skb = make_writable(skb, 0, gfp);
323 struct udphdr *th = udp_hdr(skb);
324 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
326 u16 new = a->tp_port;
327 update_csum((u16*)(skb_transport_header(skb) + check_ofs),
330 if (a->type == ODPAT_SET_TP_SRC)
331 key->tp_src = a->tp_port;
333 key->tp_dst = a->tp_port;
338 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
345 p = rcu_dereference(dp->ports[out_port]);
349 vport_send(p->vport, skb);
356 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
358 static int output_group(struct datapath *dp, __u16 group,
359 struct sk_buff *skb, gfp_t gfp)
361 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
367 for (i = 0; i < g->n_ports; i++) {
368 struct dp_port *p = rcu_dereference(dp->ports[g->ports[i]]);
369 if (!p || OVS_CB(skb)->dp_port == p)
371 if (prev_port != -1) {
372 struct sk_buff *clone = skb_clone(skb, gfp);
375 do_output(dp, clone, prev_port);
377 prev_port = p->port_no;
382 static int output_control(struct datapath *dp, struct sk_buff *skb, u32 arg,
385 skb = skb_clone(skb, gfp);
388 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
391 /* Send a copy of this packet up to the sFlow agent, along with extra
392 * information about what happened to it. */
393 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
394 const union odp_action *a, int n_actions,
395 gfp_t gfp, struct dp_port *dp_port)
397 struct odp_sflow_sample_header *hdr;
398 unsigned int actlen = n_actions * sizeof(union odp_action);
399 unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
400 struct sk_buff *nskb;
402 nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
406 memcpy(__skb_push(nskb, actlen), a, actlen);
407 hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
408 hdr->n_actions = n_actions;
409 hdr->sample_pool = atomic_read(&dp_port->sflow_pool);
410 dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
413 /* Execute a list of actions against 'skb'. */
414 int execute_actions(struct datapath *dp, struct sk_buff *skb,
415 struct odp_flow_key *key,
416 const union odp_action *a, int n_actions,
419 /* Every output action needs a separate clone of 'skb', but the common
420 * case is just a single output action, so that doing a clone and
421 * then freeing the original skbuff is wasteful. So the following code
422 * is slightly obscure just to avoid that. */
424 u32 priority = skb->priority;
427 if (dp->sflow_probability) {
428 struct dp_port *p = OVS_CB(skb)->dp_port;
430 atomic_inc(&p->sflow_pool);
431 if (dp->sflow_probability == UINT_MAX ||
432 net_random() < dp->sflow_probability)
433 sflow_sample(dp, skb, a, n_actions, gfp, p);
437 OVS_CB(skb)->tun_id = 0;
439 for (; n_actions > 0; a++, n_actions--) {
440 WARN_ON_ONCE(skb_shared(skb));
441 if (prev_port != -1) {
442 do_output(dp, skb_clone(skb, gfp), prev_port);
448 prev_port = a->output.port;
451 case ODPAT_OUTPUT_GROUP:
452 prev_port = output_group(dp, a->output_group.group,
456 case ODPAT_CONTROLLER:
457 err = output_control(dp, skb, a->controller.arg, gfp);
464 case ODPAT_SET_TUNNEL:
465 OVS_CB(skb)->tun_id = key->tun_id = a->tunnel.tun_id;
468 case ODPAT_SET_VLAN_VID:
469 case ODPAT_SET_VLAN_PCP:
470 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
475 case ODPAT_STRIP_VLAN:
476 skb = strip_vlan(skb, key, gfp);
479 case ODPAT_SET_DL_SRC:
480 case ODPAT_SET_DL_DST:
481 skb = set_dl_addr(skb, key, &a->dl_addr, gfp);
484 case ODPAT_SET_NW_SRC:
485 case ODPAT_SET_NW_DST:
486 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
489 case ODPAT_SET_NW_TOS:
490 skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
493 case ODPAT_SET_TP_SRC:
494 case ODPAT_SET_TP_DST:
495 skb = set_tp_port(skb, key, &a->tp_port, gfp);
498 case ODPAT_SET_PRIORITY:
499 skb->priority = a->priority.priority;
502 case ODPAT_POP_PRIORITY:
503 skb->priority = priority;
510 do_output(dp, skb, prev_port);