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"
26 static struct sk_buff *
27 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 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
36 /* Before 2.6.24 these fields were not copied when
37 * doing an skb_copy_expand. */
38 nskb->ip_summed = skb->ip_summed;
39 nskb->csum = skb->csum;
41 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
42 /* These fields are copied in skb_clone but not in
43 * skb_copy or related functions. We need to manually
44 * copy them over here. */
45 nskb->proto_data_valid = skb->proto_data_valid;
46 nskb->proto_csum_blank = skb->proto_csum_blank;
52 unsigned int hdr_len = (skb_transport_offset(skb)
53 + sizeof(struct tcphdr));
54 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
62 static struct sk_buff *
63 vlan_pull_tag(struct sk_buff *skb)
65 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
68 /* Verify we were given a vlan packet */
69 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
72 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
73 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
74 + ETH_HLEN, VLAN_HLEN, 0));
76 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
78 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
80 skb->protocol = eh->h_proto;
81 skb->mac_header += VLAN_HLEN;
87 static struct sk_buff *
88 modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
89 struct odp_flow_key *key, const union odp_action *a,
90 int n_actions, gfp_t gfp)
94 if (a->type == ODPAT_SET_VLAN_VID) {
95 tci = ntohs(a->vlan_vid.vlan_vid);
97 key->dl_vlan = htons(tci & mask);
99 tci = a->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT;
100 mask = VLAN_PCP_MASK;
103 skb = make_writable(skb, VLAN_HLEN, gfp);
105 return ERR_PTR(-ENOMEM);
107 if (skb->protocol == htons(ETH_P_8021Q)) {
108 /* Modify vlan id, but maintain other TCI values */
109 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
110 __be16 old_tci = vh->h_vlan_TCI;
112 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
114 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE) {
115 __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
117 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
121 /* Add vlan header */
123 /* Set up checksumming pointers for checksum-deferred packets
124 * on Xen. Otherwise, dev_queue_xmit() will try to do this
125 * when we send the packet out on the wire, and it will fail at
126 * that point because skb_checksum_setup() will not look inside
127 * an 802.1Q header. */
128 vswitch_skb_checksum_setup(skb);
130 /* GSO is not implemented for packets with an 802.1Q header, so
131 * we have to do segmentation before we add that header.
133 * GSO does work with hardware-accelerated VLAN tagging, but we
134 * can't use hardware-accelerated VLAN tagging since it
135 * requires the device to have a VLAN group configured (with
136 * e.g. vconfig(8)) and we don't do that.
138 * Having to do this here may be a performance loss, since we
139 * can't take advantage of TSO hardware support, although it
140 * does not make a measurable network performance difference
141 * for 1G Ethernet. Fixing that would require patching the
142 * kernel (either to add GSO support to the VLAN protocol or to
143 * support hardware-accelerated VLAN tagging without VLAN
144 * groups configured). */
145 if (skb_is_gso(skb)) {
146 struct sk_buff *segs;
148 segs = skb_gso_segment(skb, 0);
150 if (unlikely(IS_ERR(segs)))
151 return ERR_CAST(segs);
154 struct sk_buff *nskb = segs->next;
159 /* GSO can change the checksum type so update.*/
160 compute_ip_summed(segs, true);
162 segs = __vlan_put_tag(segs, tci);
165 struct odp_flow_key segkey = *key;
166 err = execute_actions(dp, segs,
173 while ((segs = nskb)) {
182 } while (segs->next);
185 compute_ip_summed(skb, true);
188 /* The hardware-accelerated version of vlan_put_tag() works
189 * only for a device that has a VLAN group configured (with
190 * e.g. vconfig(8)), so call the software-only version
191 * __vlan_put_tag() directly instead.
193 skb = __vlan_put_tag(skb, tci);
195 return ERR_PTR(-ENOMEM);
197 /* GSO doesn't fix up the hardware computed checksum so this
198 * will only be hit in the non-GSO case. */
199 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
200 skb->csum = csum_add(skb->csum, csum_partial(skb->data
201 + ETH_HLEN, VLAN_HLEN, 0));
207 static struct sk_buff *strip_vlan(struct sk_buff *skb,
208 struct odp_flow_key *key, gfp_t gfp)
210 skb = make_writable(skb, 0, gfp);
213 key->dl_vlan = htons(ODP_VLAN_NONE);
218 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
219 const struct odp_action_dl_addr *a,
222 skb = make_writable(skb, 0, gfp);
224 struct ethhdr *eh = eth_hdr(skb);
225 memcpy(a->type == ODPAT_SET_DL_SRC ? eh->h_source : eh->h_dest,
226 a->dl_addr, ETH_ALEN);
231 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
232 * covered by the sum has been changed from 'from' to 'to'. If set,
233 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
234 * Based on nf_proto_csum_replace4. */
235 static void update_csum(__sum16 *sum, struct sk_buff *skb,
236 __be32 from, __be32 to, int pseudohdr)
238 __be32 diff[] = { ~from, to };
240 if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
241 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
242 ~csum_unfold(*sum)));
243 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
244 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
246 } else if (pseudohdr)
247 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
251 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
252 struct odp_flow_key *key,
253 const struct odp_action_nw_addr *a,
256 if (key->dl_type != htons(ETH_P_IP))
259 skb = make_writable(skb, 0, gfp);
261 struct iphdr *nh = ip_hdr(skb);
262 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
264 u32 new = a->nw_addr;
266 if (key->nw_proto == IPPROTO_TCP) {
267 struct tcphdr *th = tcp_hdr(skb);
268 update_csum(&th->check, skb, old, new, 1);
269 } else if (key->nw_proto == IPPROTO_UDP) {
270 struct udphdr *th = udp_hdr(skb);
271 update_csum(&th->check, skb, old, new, 1);
273 update_csum(&nh->check, skb, old, new, 0);
279 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
280 struct odp_flow_key *key,
281 const struct odp_action_nw_tos *a,
284 if (key->dl_type != htons(ETH_P_IP))
287 skb = make_writable(skb, 0, gfp);
289 struct iphdr *nh = ip_hdr(skb);
294 /* Set the DSCP bits and preserve the ECN bits. */
295 new = (a->nw_tos & ~INET_ECN_MASK) | (nh->tos & INET_ECN_MASK);
296 update_csum(&nh->check, skb, htons((uint16_t)old),
297 htons((uint16_t)new), 0);
303 static struct sk_buff *
304 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
305 const struct odp_action_tp_port *a,
310 if (key->dl_type != htons(ETH_P_IP))
313 if (key->nw_proto == IPPROTO_TCP)
314 check_ofs = offsetof(struct tcphdr, check);
315 else if (key->nw_proto == IPPROTO_UDP)
316 check_ofs = offsetof(struct udphdr, check);
320 skb = make_writable(skb, 0, gfp);
322 struct udphdr *th = udp_hdr(skb);
323 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
325 u16 new = a->tp_port;
326 update_csum((u16*)(skb_transport_header(skb) + check_ofs),
333 static inline unsigned packet_length(const struct sk_buff *skb)
335 unsigned length = skb->len - ETH_HLEN;
336 if (skb->protocol == htons(ETH_P_8021Q))
341 int dp_xmit_skb(struct sk_buff *skb)
343 struct datapath *dp = skb->dev->br_port->dp;
346 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) {
347 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
348 dp_name(dp), packet_length(skb), skb->dev->mtu);
353 forward_ip_summed(skb);
360 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
362 struct net_bridge_port *p;
363 struct net_device *dev;
368 p = dp->ports[out_port];
372 dev = skb->dev = p->dev;
374 dp_dev_recv(dev, skb);
383 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
385 static int output_group(struct datapath *dp, __u16 group,
386 struct sk_buff *skb, gfp_t gfp)
388 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
394 for (i = 0; i < g->n_ports; i++) {
395 struct net_bridge_port *p = dp->ports[g->ports[i]];
396 if (!p || skb->dev == p->dev)
398 if (prev_port != -1) {
399 struct sk_buff *clone = skb_clone(skb, gfp);
402 do_output(dp, clone, prev_port);
404 prev_port = p->port_no;
410 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
412 skb = skb_clone(skb, gfp);
415 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
418 /* Send a copy of this packet up to the sFlow agent, along with extra
419 * information about what happened to it. */
420 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
421 const union odp_action *a, int n_actions,
422 gfp_t gfp, struct net_bridge_port *nbp)
424 struct odp_sflow_sample_header *hdr;
425 unsigned int actlen = n_actions * sizeof(union odp_action);
426 unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
427 struct sk_buff *nskb;
429 nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
433 memcpy(__skb_push(nskb, actlen), a, actlen);
434 hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
435 hdr->n_actions = n_actions;
436 hdr->sample_pool = atomic_read(&nbp->sflow_pool);
437 dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
440 /* Execute a list of actions against 'skb'. */
441 int execute_actions(struct datapath *dp, struct sk_buff *skb,
442 struct odp_flow_key *key,
443 const union odp_action *a, int n_actions,
446 /* Every output action needs a separate clone of 'skb', but the common
447 * case is just a single output action, so that doing a clone and
448 * then freeing the original skbuff is wasteful. So the following code
449 * is slightly obscure just to avoid that. */
453 if (dp->sflow_probability) {
454 struct net_bridge_port *p = skb->dev->br_port;
456 atomic_inc(&p->sflow_pool);
457 if (dp->sflow_probability == UINT_MAX ||
458 net_random() < dp->sflow_probability)
459 sflow_sample(dp, skb, a, n_actions, gfp, p);
463 for (; n_actions > 0; a++, n_actions--) {
464 WARN_ON_ONCE(skb_shared(skb));
465 if (prev_port != -1) {
466 do_output(dp, skb_clone(skb, gfp), prev_port);
472 prev_port = a->output.port;
475 case ODPAT_OUTPUT_GROUP:
476 prev_port = output_group(dp, a->output_group.group,
480 case ODPAT_CONTROLLER:
481 err = output_control(dp, skb, a->controller.arg, gfp);
488 case ODPAT_SET_VLAN_VID:
489 case ODPAT_SET_VLAN_PCP:
490 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
495 case ODPAT_STRIP_VLAN:
496 skb = strip_vlan(skb, key, gfp);
499 case ODPAT_SET_DL_SRC:
500 case ODPAT_SET_DL_DST:
501 skb = set_dl_addr(skb, &a->dl_addr, gfp);
504 case ODPAT_SET_NW_SRC:
505 case ODPAT_SET_NW_DST:
506 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
509 case ODPAT_SET_NW_TOS:
510 skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
513 case ODPAT_SET_TP_SRC:
514 case ODPAT_SET_TP_DST:
515 skb = set_tp_port(skb, key, &a->tp_port, gfp);
522 do_output(dp, skb, prev_port);