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 void set_tunnel(struct sk_buff *skb, struct odp_flow_key *key,
52 OVS_CB(skb)->tun_id = key->tun_id = tun_id;
55 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
57 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
60 /* Verify we were given a vlan packet */
61 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
64 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
65 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
66 + ETH_HLEN, VLAN_HLEN, 0));
68 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
70 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
72 skb->protocol = eh->h_proto;
73 skb->mac_header += VLAN_HLEN;
79 static struct sk_buff *modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
80 struct odp_flow_key *key, const union odp_action *a,
81 int n_actions, gfp_t gfp)
85 if (a->type == ODPAT_SET_VLAN_VID) {
86 tci = ntohs(a->vlan_vid.vlan_vid);
88 key->dl_vlan = a->vlan_vid.vlan_vid;
90 tci = a->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT;
92 key->dl_vlan_pcp = a->vlan_pcp.vlan_pcp;
95 skb = make_writable(skb, VLAN_HLEN, gfp);
97 return ERR_PTR(-ENOMEM);
99 if (skb->protocol == htons(ETH_P_8021Q)) {
100 /* Modify vlan id, but maintain other TCI values */
101 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
102 __be16 old_tci = vh->h_vlan_TCI;
104 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
106 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE) {
107 __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
109 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
115 /* Add vlan header */
117 /* Set up checksumming pointers for checksum-deferred packets
118 * on Xen. Otherwise, dev_queue_xmit() will try to do this
119 * when we send the packet out on the wire, and it will fail at
120 * that point because skb_checksum_setup() will not look inside
121 * an 802.1Q header. */
122 err = vswitch_skb_checksum_setup(skb);
128 /* GSO is not implemented for packets with an 802.1Q header, so
129 * we have to do segmentation before we add that header.
131 * GSO does work with hardware-accelerated VLAN tagging, but we
132 * can't use hardware-accelerated VLAN tagging since it
133 * requires the device to have a VLAN group configured (with
134 * e.g. vconfig(8)) and we don't do that.
136 * Having to do this here may be a performance loss, since we
137 * can't take advantage of TSO hardware support, although it
138 * does not make a measurable network performance difference
139 * for 1G Ethernet. Fixing that would require patching the
140 * kernel (either to add GSO support to the VLAN protocol or to
141 * support hardware-accelerated VLAN tagging without VLAN
142 * groups configured). */
143 if (skb_is_gso(skb)) {
144 struct sk_buff *segs;
146 segs = skb_gso_segment(skb, 0);
148 if (unlikely(IS_ERR(segs)))
149 return ERR_CAST(segs);
152 struct sk_buff *nskb = segs->next;
157 /* GSO can change the checksum type so update.*/
158 compute_ip_summed(segs, true);
160 segs = __vlan_put_tag(segs, tci);
163 struct odp_flow_key segkey = *key;
164 err = execute_actions(dp, segs,
171 while ((segs = nskb)) {
180 } while (segs->next);
183 compute_ip_summed(skb, true);
186 /* The hardware-accelerated version of vlan_put_tag() works
187 * only for a device that has a VLAN group configured (with
188 * e.g. vconfig(8)), so call the software-only version
189 * __vlan_put_tag() directly instead.
191 skb = __vlan_put_tag(skb, tci);
193 return ERR_PTR(-ENOMEM);
195 /* GSO doesn't fix up the hardware computed checksum so this
196 * will only be hit in the non-GSO case. */
197 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
198 skb->csum = csum_add(skb->csum, csum_partial(skb->data
199 + ETH_HLEN, VLAN_HLEN, 0));
205 static struct sk_buff *strip_vlan(struct sk_buff *skb,
206 struct odp_flow_key *key, gfp_t gfp)
208 skb = make_writable(skb, 0, gfp);
211 key->dl_vlan = htons(ODP_VLAN_NONE);
216 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
217 struct odp_flow_key *key,
218 const struct odp_action_dl_addr *a,
221 skb = make_writable(skb, 0, gfp);
223 struct ethhdr *eh = eth_hdr(skb);
224 if (a->type == ODPAT_SET_DL_SRC) {
225 memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
226 memcpy(key->dl_src, a->dl_addr, ETH_ALEN);
228 memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
229 memcpy(key->dl_dst, a->dl_addr, ETH_ALEN);
235 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
236 * covered by the sum has been changed from 'from' to 'to'. If set,
237 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
238 * Based on nf_proto_csum_replace4. */
239 static void update_csum(__sum16 *sum, struct sk_buff *skb,
240 __be32 from, __be32 to, int pseudohdr)
242 __be32 diff[] = { ~from, to };
244 if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
245 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
246 ~csum_unfold(*sum)));
247 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
248 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
250 } else if (pseudohdr)
251 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
255 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
256 struct odp_flow_key *key,
257 const struct odp_action_nw_addr *a,
260 if (key->dl_type != htons(ETH_P_IP))
263 skb = make_writable(skb, 0, gfp);
265 struct iphdr *nh = ip_hdr(skb);
266 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
268 u32 new = a->nw_addr;
270 if (key->nw_proto == IPPROTO_TCP) {
271 struct tcphdr *th = tcp_hdr(skb);
272 update_csum(&th->check, skb, old, new, 1);
273 } else if (key->nw_proto == IPPROTO_UDP) {
274 struct udphdr *th = udp_hdr(skb);
275 update_csum(&th->check, skb, old, new, 1);
277 update_csum(&nh->check, skb, old, new, 0);
280 if (a->type == ODPAT_SET_NW_SRC)
281 key->nw_src = a->nw_addr;
283 key->nw_dst = a->nw_addr;
288 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
289 struct odp_flow_key *key,
290 const struct odp_action_nw_tos *a,
293 if (key->dl_type != htons(ETH_P_IP))
296 skb = make_writable(skb, 0, gfp);
298 struct iphdr *nh = ip_hdr(skb);
303 /* Set the DSCP bits and preserve the ECN bits. */
304 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
305 update_csum(&nh->check, skb, htons((uint16_t)old),
306 htons((uint16_t)new), 0);
308 key->nw_tos = a->nw_tos;
313 static struct sk_buff *set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
314 const struct odp_action_tp_port *a, gfp_t gfp)
318 if (key->dl_type != htons(ETH_P_IP))
321 if (key->nw_proto == IPPROTO_TCP)
322 check_ofs = offsetof(struct tcphdr, check);
323 else if (key->nw_proto == IPPROTO_UDP)
324 check_ofs = offsetof(struct udphdr, check);
328 skb = make_writable(skb, 0, gfp);
330 struct udphdr *th = udp_hdr(skb);
331 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
333 u16 new = a->tp_port;
334 update_csum((u16*)(skb_transport_header(skb) + check_ofs),
337 if (a->type == ODPAT_SET_TP_SRC)
338 key->tp_src = a->tp_port;
340 key->tp_dst = a->tp_port;
345 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
352 p = rcu_dereference(dp->ports[out_port]);
356 vport_send(p->vport, skb);
363 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
365 static int output_group(struct datapath *dp, __u16 group,
366 struct sk_buff *skb, gfp_t gfp)
368 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
374 for (i = 0; i < g->n_ports; i++) {
375 struct dp_port *p = rcu_dereference(dp->ports[g->ports[i]]);
376 if (!p || OVS_CB(skb)->dp_port == p)
378 if (prev_port != -1) {
379 struct sk_buff *clone = skb_clone(skb, gfp);
382 do_output(dp, clone, prev_port);
384 prev_port = p->port_no;
389 static int output_control(struct datapath *dp, struct sk_buff *skb, u32 arg,
392 skb = skb_clone(skb, gfp);
395 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
398 /* Send a copy of this packet up to the sFlow agent, along with extra
399 * information about what happened to it. */
400 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
401 const union odp_action *a, int n_actions,
402 gfp_t gfp, struct dp_port *dp_port)
404 struct odp_sflow_sample_header *hdr;
405 unsigned int actlen = n_actions * sizeof(union odp_action);
406 unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
407 struct sk_buff *nskb;
409 nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
413 memcpy(__skb_push(nskb, actlen), a, actlen);
414 hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
415 hdr->n_actions = n_actions;
416 hdr->sample_pool = atomic_read(&dp_port->sflow_pool);
417 dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
420 /* Execute a list of actions against 'skb'. */
421 int execute_actions(struct datapath *dp, struct sk_buff *skb,
422 struct odp_flow_key *key,
423 const union odp_action *a, int n_actions,
426 /* Every output action needs a separate clone of 'skb', but the common
427 * case is just a single output action, so that doing a clone and
428 * then freeing the original skbuff is wasteful. So the following code
429 * is slightly obscure just to avoid that. */
431 u32 priority = skb->priority;
434 if (dp->sflow_probability) {
435 struct dp_port *p = OVS_CB(skb)->dp_port;
437 atomic_inc(&p->sflow_pool);
438 if (dp->sflow_probability == UINT_MAX ||
439 net_random() < dp->sflow_probability)
440 sflow_sample(dp, skb, a, n_actions, gfp, p);
444 OVS_CB(skb)->tun_id = 0;
446 for (; n_actions > 0; a++, n_actions--) {
447 WARN_ON_ONCE(skb_shared(skb));
448 if (prev_port != -1) {
449 do_output(dp, skb_clone(skb, gfp), prev_port);
455 prev_port = a->output.port;
458 case ODPAT_OUTPUT_GROUP:
459 prev_port = output_group(dp, a->output_group.group,
463 case ODPAT_CONTROLLER:
464 err = output_control(dp, skb, a->controller.arg, gfp);
471 case ODPAT_SET_TUNNEL:
472 set_tunnel(skb, key, a->tunnel.tun_id);
475 case ODPAT_SET_VLAN_VID:
476 case ODPAT_SET_VLAN_PCP:
477 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
482 case ODPAT_STRIP_VLAN:
483 skb = strip_vlan(skb, key, gfp);
486 case ODPAT_SET_DL_SRC:
487 case ODPAT_SET_DL_DST:
488 skb = set_dl_addr(skb, key, &a->dl_addr, gfp);
491 case ODPAT_SET_NW_SRC:
492 case ODPAT_SET_NW_DST:
493 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
496 case ODPAT_SET_NW_TOS:
497 skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
500 case ODPAT_SET_TP_SRC:
501 case ODPAT_SET_TP_DST:
502 skb = set_tp_port(skb, key, &a->tp_port, gfp);
505 case ODPAT_SET_PRIORITY:
506 skb->priority = a->priority.priority;
509 case ODPAT_POP_PRIORITY:
510 skb->priority = priority;
517 do_output(dp, skb, prev_port);