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 *
28 make_writable(struct sk_buff *skb, unsigned min_headroom, gfp_t gfp)
30 if (skb_shared(skb) || skb_cloned(skb)) {
32 unsigned headroom = max(min_headroom, skb_headroom(skb));
34 nskb = skb_copy_expand(skb, headroom, skb_tailroom(skb), gfp);
36 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
37 /* Before 2.6.24 these fields were not copied when
38 * doing an skb_copy_expand. */
39 nskb->ip_summed = skb->ip_summed;
40 nskb->csum = skb->csum;
42 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
43 /* These fields are copied in skb_clone but not in
44 * skb_copy or related functions. We need to manually
45 * copy them over here. */
46 nskb->proto_data_valid = skb->proto_data_valid;
47 nskb->proto_csum_blank = skb->proto_csum_blank;
53 unsigned int hdr_len = (skb_transport_offset(skb)
54 + sizeof(struct tcphdr));
55 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
62 static void set_tunnel(struct sk_buff *skb, struct odp_flow_key *key,
65 OVS_CB(skb)->tun_id = key->tun_id = tun_id;
68 static struct sk_buff *
69 vlan_pull_tag(struct sk_buff *skb)
71 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
74 /* Verify we were given a vlan packet */
75 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
78 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
79 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
80 + ETH_HLEN, VLAN_HLEN, 0));
82 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
84 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
86 skb->protocol = eh->h_proto;
87 skb->mac_header += VLAN_HLEN;
93 static struct sk_buff *
94 modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
95 struct odp_flow_key *key, const union odp_action *a,
96 int n_actions, gfp_t gfp)
100 if (a->type == ODPAT_SET_VLAN_VID) {
101 tci = ntohs(a->vlan_vid.vlan_vid);
102 mask = VLAN_VID_MASK;
103 key->dl_vlan = a->vlan_vid.vlan_vid;
105 tci = a->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT;
106 mask = VLAN_PCP_MASK;
107 key->dl_vlan_pcp = a->vlan_pcp.vlan_pcp;
110 skb = make_writable(skb, VLAN_HLEN, gfp);
112 return ERR_PTR(-ENOMEM);
114 if (skb->protocol == htons(ETH_P_8021Q)) {
115 /* Modify vlan id, but maintain other TCI values */
116 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
117 __be16 old_tci = vh->h_vlan_TCI;
119 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
121 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE) {
122 __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
124 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
128 /* Add vlan header */
130 /* Set up checksumming pointers for checksum-deferred packets
131 * on Xen. Otherwise, dev_queue_xmit() will try to do this
132 * when we send the packet out on the wire, and it will fail at
133 * that point because skb_checksum_setup() will not look inside
134 * an 802.1Q header. */
135 vswitch_skb_checksum_setup(skb);
137 /* GSO is not implemented for packets with an 802.1Q header, so
138 * we have to do segmentation before we add that header.
140 * GSO does work with hardware-accelerated VLAN tagging, but we
141 * can't use hardware-accelerated VLAN tagging since it
142 * requires the device to have a VLAN group configured (with
143 * e.g. vconfig(8)) and we don't do that.
145 * Having to do this here may be a performance loss, since we
146 * can't take advantage of TSO hardware support, although it
147 * does not make a measurable network performance difference
148 * for 1G Ethernet. Fixing that would require patching the
149 * kernel (either to add GSO support to the VLAN protocol or to
150 * support hardware-accelerated VLAN tagging without VLAN
151 * groups configured). */
152 if (skb_is_gso(skb)) {
153 struct sk_buff *segs;
155 segs = skb_gso_segment(skb, 0);
157 if (unlikely(IS_ERR(segs)))
158 return ERR_CAST(segs);
161 struct sk_buff *nskb = segs->next;
166 /* GSO can change the checksum type so update.*/
167 compute_ip_summed(segs, true);
169 segs = __vlan_put_tag(segs, tci);
172 struct odp_flow_key segkey = *key;
173 err = execute_actions(dp, segs,
180 while ((segs = nskb)) {
189 } while (segs->next);
192 compute_ip_summed(skb, true);
195 /* The hardware-accelerated version of vlan_put_tag() works
196 * only for a device that has a VLAN group configured (with
197 * e.g. vconfig(8)), so call the software-only version
198 * __vlan_put_tag() directly instead.
200 skb = __vlan_put_tag(skb, tci);
202 return ERR_PTR(-ENOMEM);
204 /* GSO doesn't fix up the hardware computed checksum so this
205 * will only be hit in the non-GSO case. */
206 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
207 skb->csum = csum_add(skb->csum, csum_partial(skb->data
208 + ETH_HLEN, VLAN_HLEN, 0));
214 static struct sk_buff *strip_vlan(struct sk_buff *skb,
215 struct odp_flow_key *key, gfp_t gfp)
217 skb = make_writable(skb, 0, gfp);
220 key->dl_vlan = htons(ODP_VLAN_NONE);
225 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
226 struct odp_flow_key *key,
227 const struct odp_action_dl_addr *a,
230 skb = make_writable(skb, 0, gfp);
232 struct ethhdr *eh = eth_hdr(skb);
233 if (a->type == ODPAT_SET_DL_SRC) {
234 memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
235 memcpy(key->dl_src, a->dl_addr, ETH_ALEN);
237 memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
238 memcpy(key->dl_dst, a->dl_addr, ETH_ALEN);
244 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
245 * covered by the sum has been changed from 'from' to 'to'. If set,
246 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
247 * Based on nf_proto_csum_replace4. */
248 static void update_csum(__sum16 *sum, struct sk_buff *skb,
249 __be32 from, __be32 to, int pseudohdr)
251 __be32 diff[] = { ~from, to };
253 if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
254 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
255 ~csum_unfold(*sum)));
256 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
257 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
259 } else if (pseudohdr)
260 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
264 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
265 struct odp_flow_key *key,
266 const struct odp_action_nw_addr *a,
269 if (key->dl_type != htons(ETH_P_IP))
272 skb = make_writable(skb, 0, gfp);
274 struct iphdr *nh = ip_hdr(skb);
275 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
277 u32 new = a->nw_addr;
279 if (key->nw_proto == IPPROTO_TCP) {
280 struct tcphdr *th = tcp_hdr(skb);
281 update_csum(&th->check, skb, old, new, 1);
282 } else if (key->nw_proto == IPPROTO_UDP) {
283 struct udphdr *th = udp_hdr(skb);
284 update_csum(&th->check, skb, old, new, 1);
286 update_csum(&nh->check, skb, old, new, 0);
289 if (a->type == ODPAT_SET_NW_SRC)
290 key->nw_src = a->nw_addr;
292 key->nw_dst = a->nw_addr;
297 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
298 struct odp_flow_key *key,
299 const struct odp_action_nw_tos *a,
302 if (key->dl_type != htons(ETH_P_IP))
305 skb = make_writable(skb, 0, gfp);
307 struct iphdr *nh = ip_hdr(skb);
312 /* Set the DSCP bits and preserve the ECN bits. */
313 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
314 update_csum(&nh->check, skb, htons((uint16_t)old),
315 htons((uint16_t)new), 0);
317 key->nw_tos = a->nw_tos;
322 static struct sk_buff *
323 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
324 const struct odp_action_tp_port *a,
329 if (key->dl_type != htons(ETH_P_IP))
332 if (key->nw_proto == IPPROTO_TCP)
333 check_ofs = offsetof(struct tcphdr, check);
334 else if (key->nw_proto == IPPROTO_UDP)
335 check_ofs = offsetof(struct udphdr, check);
339 skb = make_writable(skb, 0, gfp);
341 struct udphdr *th = udp_hdr(skb);
342 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
344 u16 new = a->tp_port;
345 update_csum((u16*)(skb_transport_header(skb) + check_ofs),
348 if (a->type == ODPAT_SET_TP_SRC)
349 key->tp_src = a->tp_port;
351 key->tp_dst = a->tp_port;
356 static inline unsigned packet_length(const struct sk_buff *skb)
358 unsigned length = skb->len - ETH_HLEN;
359 if (skb->protocol == htons(ETH_P_8021Q))
365 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
373 p = rcu_dereference(dp->ports[out_port]);
377 mtu = vport_get_mtu(p->vport);
378 if (packet_length(skb) > mtu && !skb_is_gso(skb)) {
379 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
380 dp_name(dp), packet_length(skb), mtu);
384 vport_send(p->vport, skb);
391 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
393 static int output_group(struct datapath *dp, __u16 group,
394 struct sk_buff *skb, gfp_t gfp)
396 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
402 for (i = 0; i < g->n_ports; i++) {
403 struct dp_port *p = rcu_dereference(dp->ports[g->ports[i]]);
404 if (!p || OVS_CB(skb)->dp_port == p)
406 if (prev_port != -1) {
407 struct sk_buff *clone = skb_clone(skb, gfp);
410 do_output(dp, clone, prev_port);
412 prev_port = p->port_no;
418 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
420 skb = skb_clone(skb, gfp);
423 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
426 /* Send a copy of this packet up to the sFlow agent, along with extra
427 * information about what happened to it. */
428 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
429 const union odp_action *a, int n_actions,
430 gfp_t gfp, struct dp_port *dp_port)
432 struct odp_sflow_sample_header *hdr;
433 unsigned int actlen = n_actions * sizeof(union odp_action);
434 unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
435 struct sk_buff *nskb;
437 nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
441 memcpy(__skb_push(nskb, actlen), a, actlen);
442 hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
443 hdr->n_actions = n_actions;
444 hdr->sample_pool = atomic_read(&dp_port->sflow_pool);
445 dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
448 /* Execute a list of actions against 'skb'. */
449 int execute_actions(struct datapath *dp, struct sk_buff *skb,
450 struct odp_flow_key *key,
451 const union odp_action *a, int n_actions,
454 /* Every output action needs a separate clone of 'skb', but the common
455 * case is just a single output action, so that doing a clone and
456 * then freeing the original skbuff is wasteful. So the following code
457 * is slightly obscure just to avoid that. */
461 if (dp->sflow_probability) {
462 struct dp_port *p = OVS_CB(skb)->dp_port;
464 atomic_inc(&p->sflow_pool);
465 if (dp->sflow_probability == UINT_MAX ||
466 net_random() < dp->sflow_probability)
467 sflow_sample(dp, skb, a, n_actions, gfp, p);
471 OVS_CB(skb)->tun_id = 0;
473 for (; n_actions > 0; a++, n_actions--) {
474 WARN_ON_ONCE(skb_shared(skb));
475 if (prev_port != -1) {
476 do_output(dp, skb_clone(skb, gfp), prev_port);
482 prev_port = a->output.port;
485 case ODPAT_OUTPUT_GROUP:
486 prev_port = output_group(dp, a->output_group.group,
490 case ODPAT_CONTROLLER:
491 err = output_control(dp, skb, a->controller.arg, gfp);
498 case ODPAT_SET_TUNNEL:
499 set_tunnel(skb, key, a->tunnel.tun_id);
502 case ODPAT_SET_VLAN_VID:
503 case ODPAT_SET_VLAN_PCP:
504 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
509 case ODPAT_STRIP_VLAN:
510 skb = strip_vlan(skb, key, gfp);
513 case ODPAT_SET_DL_SRC:
514 case ODPAT_SET_DL_DST:
515 skb = set_dl_addr(skb, key, &a->dl_addr, gfp);
518 case ODPAT_SET_NW_SRC:
519 case ODPAT_SET_NW_DST:
520 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
523 case ODPAT_SET_NW_TOS:
524 skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
527 case ODPAT_SET_TP_SRC:
528 case ODPAT_SET_TP_DST:
529 skb = set_tp_port(skb, key, &a->tp_port, gfp);
536 do_output(dp, skb, prev_port);