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 set_skb_csum_bits(skb, nskb);
41 unsigned int hdr_len = (skb_transport_offset(skb)
42 + sizeof(struct tcphdr));
43 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
50 static void set_tunnel(struct sk_buff *skb, struct odp_flow_key *key,
53 OVS_CB(skb)->tun_id = key->tun_id = tun_id;
56 static struct sk_buff *
57 vlan_pull_tag(struct sk_buff *skb)
59 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
62 /* Verify we were given a vlan packet */
63 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
66 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
67 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
68 + ETH_HLEN, VLAN_HLEN, 0));
70 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
72 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
74 skb->protocol = eh->h_proto;
75 skb->mac_header += VLAN_HLEN;
81 static struct sk_buff *
82 modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
83 struct odp_flow_key *key, const union odp_action *a,
84 int n_actions, gfp_t gfp)
88 if (a->type == ODPAT_SET_VLAN_VID) {
89 tci = ntohs(a->vlan_vid.vlan_vid);
91 key->dl_vlan = a->vlan_vid.vlan_vid;
93 tci = a->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT;
95 key->dl_vlan_pcp = a->vlan_pcp.vlan_pcp;
98 skb = make_writable(skb, VLAN_HLEN, gfp);
100 return ERR_PTR(-ENOMEM);
102 if (skb->protocol == htons(ETH_P_8021Q)) {
103 /* Modify vlan id, but maintain other TCI values */
104 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
105 __be16 old_tci = vh->h_vlan_TCI;
107 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
109 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE) {
110 __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
112 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
118 /* Add vlan header */
120 /* Set up checksumming pointers for checksum-deferred packets
121 * on Xen. Otherwise, dev_queue_xmit() will try to do this
122 * when we send the packet out on the wire, and it will fail at
123 * that point because skb_checksum_setup() will not look inside
124 * an 802.1Q header. */
125 err = vswitch_skb_checksum_setup(skb);
131 /* GSO is not implemented for packets with an 802.1Q header, so
132 * we have to do segmentation before we add that header.
134 * GSO does work with hardware-accelerated VLAN tagging, but we
135 * can't use hardware-accelerated VLAN tagging since it
136 * requires the device to have a VLAN group configured (with
137 * e.g. vconfig(8)) and we don't do that.
139 * Having to do this here may be a performance loss, since we
140 * can't take advantage of TSO hardware support, although it
141 * does not make a measurable network performance difference
142 * for 1G Ethernet. Fixing that would require patching the
143 * kernel (either to add GSO support to the VLAN protocol or to
144 * support hardware-accelerated VLAN tagging without VLAN
145 * groups configured). */
146 if (skb_is_gso(skb)) {
147 struct sk_buff *segs;
149 segs = skb_gso_segment(skb, 0);
151 if (unlikely(IS_ERR(segs)))
152 return ERR_CAST(segs);
155 struct sk_buff *nskb = segs->next;
160 /* GSO can change the checksum type so update.*/
161 compute_ip_summed(segs, true);
163 segs = __vlan_put_tag(segs, tci);
166 struct odp_flow_key segkey = *key;
167 err = execute_actions(dp, segs,
174 while ((segs = nskb)) {
183 } while (segs->next);
186 compute_ip_summed(skb, true);
189 /* The hardware-accelerated version of vlan_put_tag() works
190 * only for a device that has a VLAN group configured (with
191 * e.g. vconfig(8)), so call the software-only version
192 * __vlan_put_tag() directly instead.
194 skb = __vlan_put_tag(skb, tci);
196 return ERR_PTR(-ENOMEM);
198 /* GSO doesn't fix up the hardware computed checksum so this
199 * will only be hit in the non-GSO case. */
200 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
201 skb->csum = csum_add(skb->csum, csum_partial(skb->data
202 + ETH_HLEN, VLAN_HLEN, 0));
208 static struct sk_buff *strip_vlan(struct sk_buff *skb,
209 struct odp_flow_key *key, gfp_t gfp)
211 skb = make_writable(skb, 0, gfp);
214 key->dl_vlan = htons(ODP_VLAN_NONE);
219 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
220 struct odp_flow_key *key,
221 const struct odp_action_dl_addr *a,
224 skb = make_writable(skb, 0, gfp);
226 struct ethhdr *eh = eth_hdr(skb);
227 if (a->type == ODPAT_SET_DL_SRC) {
228 memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
229 memcpy(key->dl_src, a->dl_addr, ETH_ALEN);
231 memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
232 memcpy(key->dl_dst, a->dl_addr, ETH_ALEN);
238 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
239 * covered by the sum has been changed from 'from' to 'to'. If set,
240 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
241 * Based on nf_proto_csum_replace4. */
242 static void update_csum(__sum16 *sum, struct sk_buff *skb,
243 __be32 from, __be32 to, int pseudohdr)
245 __be32 diff[] = { ~from, to };
247 if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
248 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
249 ~csum_unfold(*sum)));
250 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
251 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
253 } else if (pseudohdr)
254 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
258 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
259 struct odp_flow_key *key,
260 const struct odp_action_nw_addr *a,
263 if (key->dl_type != htons(ETH_P_IP))
266 skb = make_writable(skb, 0, gfp);
268 struct iphdr *nh = ip_hdr(skb);
269 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
271 u32 new = a->nw_addr;
273 if (key->nw_proto == IPPROTO_TCP) {
274 struct tcphdr *th = tcp_hdr(skb);
275 update_csum(&th->check, skb, old, new, 1);
276 } else if (key->nw_proto == IPPROTO_UDP) {
277 struct udphdr *th = udp_hdr(skb);
278 update_csum(&th->check, skb, old, new, 1);
280 update_csum(&nh->check, skb, old, new, 0);
283 if (a->type == ODPAT_SET_NW_SRC)
284 key->nw_src = a->nw_addr;
286 key->nw_dst = a->nw_addr;
291 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
292 struct odp_flow_key *key,
293 const struct odp_action_nw_tos *a,
296 if (key->dl_type != htons(ETH_P_IP))
299 skb = make_writable(skb, 0, gfp);
301 struct iphdr *nh = ip_hdr(skb);
306 /* Set the DSCP bits and preserve the ECN bits. */
307 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
308 update_csum(&nh->check, skb, htons((uint16_t)old),
309 htons((uint16_t)new), 0);
311 key->nw_tos = a->nw_tos;
316 static struct sk_buff *
317 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
318 const struct odp_action_tp_port *a,
323 if (key->dl_type != htons(ETH_P_IP))
326 if (key->nw_proto == IPPROTO_TCP)
327 check_ofs = offsetof(struct tcphdr, check);
328 else if (key->nw_proto == IPPROTO_UDP)
329 check_ofs = offsetof(struct udphdr, check);
333 skb = make_writable(skb, 0, gfp);
335 struct udphdr *th = udp_hdr(skb);
336 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
338 u16 new = a->tp_port;
339 update_csum((u16*)(skb_transport_header(skb) + check_ofs),
342 if (a->type == ODPAT_SET_TP_SRC)
343 key->tp_src = a->tp_port;
345 key->tp_dst = a->tp_port;
350 static inline unsigned packet_length(const struct sk_buff *skb)
352 unsigned length = skb->len - ETH_HLEN;
353 if (skb->protocol == htons(ETH_P_8021Q))
359 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
367 p = rcu_dereference(dp->ports[out_port]);
371 mtu = vport_get_mtu(p->vport);
372 if (packet_length(skb) > mtu && !skb_is_gso(skb)) {
373 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
374 dp_name(dp), packet_length(skb), mtu);
378 vport_send(p->vport, skb);
385 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
387 static int output_group(struct datapath *dp, __u16 group,
388 struct sk_buff *skb, gfp_t gfp)
390 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
396 for (i = 0; i < g->n_ports; i++) {
397 struct dp_port *p = rcu_dereference(dp->ports[g->ports[i]]);
398 if (!p || OVS_CB(skb)->dp_port == p)
400 if (prev_port != -1) {
401 struct sk_buff *clone = skb_clone(skb, gfp);
404 do_output(dp, clone, prev_port);
406 prev_port = p->port_no;
412 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
414 skb = skb_clone(skb, gfp);
417 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
420 /* Send a copy of this packet up to the sFlow agent, along with extra
421 * information about what happened to it. */
422 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
423 const union odp_action *a, int n_actions,
424 gfp_t gfp, struct dp_port *dp_port)
426 struct odp_sflow_sample_header *hdr;
427 unsigned int actlen = n_actions * sizeof(union odp_action);
428 unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
429 struct sk_buff *nskb;
431 nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
435 memcpy(__skb_push(nskb, actlen), a, actlen);
436 hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
437 hdr->n_actions = n_actions;
438 hdr->sample_pool = atomic_read(&dp_port->sflow_pool);
439 dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
442 /* Execute a list of actions against 'skb'. */
443 int execute_actions(struct datapath *dp, struct sk_buff *skb,
444 struct odp_flow_key *key,
445 const union odp_action *a, int n_actions,
448 /* Every output action needs a separate clone of 'skb', but the common
449 * case is just a single output action, so that doing a clone and
450 * then freeing the original skbuff is wasteful. So the following code
451 * is slightly obscure just to avoid that. */
453 u32 priority = skb->priority;
456 if (dp->sflow_probability) {
457 struct dp_port *p = OVS_CB(skb)->dp_port;
459 atomic_inc(&p->sflow_pool);
460 if (dp->sflow_probability == UINT_MAX ||
461 net_random() < dp->sflow_probability)
462 sflow_sample(dp, skb, a, n_actions, gfp, p);
466 OVS_CB(skb)->tun_id = 0;
468 for (; n_actions > 0; a++, n_actions--) {
469 WARN_ON_ONCE(skb_shared(skb));
470 if (prev_port != -1) {
471 do_output(dp, skb_clone(skb, gfp), prev_port);
477 prev_port = a->output.port;
480 case ODPAT_OUTPUT_GROUP:
481 prev_port = output_group(dp, a->output_group.group,
485 case ODPAT_CONTROLLER:
486 err = output_control(dp, skb, a->controller.arg, gfp);
493 case ODPAT_SET_TUNNEL:
494 set_tunnel(skb, key, a->tunnel.tun_id);
497 case ODPAT_SET_VLAN_VID:
498 case ODPAT_SET_VLAN_PCP:
499 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
504 case ODPAT_STRIP_VLAN:
505 skb = strip_vlan(skb, key, gfp);
508 case ODPAT_SET_DL_SRC:
509 case ODPAT_SET_DL_DST:
510 skb = set_dl_addr(skb, key, &a->dl_addr, gfp);
513 case ODPAT_SET_NW_SRC:
514 case ODPAT_SET_NW_DST:
515 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
518 case ODPAT_SET_NW_TOS:
519 skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
522 case ODPAT_SET_TP_SRC:
523 case ODPAT_SET_TP_DST:
524 skb = set_tp_port(skb, key, &a->tp_port, gfp);
527 case ODPAT_SET_PRIORITY:
528 skb->priority = a->priority.priority;
531 case ODPAT_POP_PRIORITY:
532 skb->priority = priority;
539 do_output(dp, skb, prev_port);