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),
116 /* Add vlan header */
118 /* Set up checksumming pointers for checksum-deferred packets
119 * on Xen. Otherwise, dev_queue_xmit() will try to do this
120 * when we send the packet out on the wire, and it will fail at
121 * that point because skb_checksum_setup() will not look inside
122 * an 802.1Q header. */
123 vswitch_skb_checksum_setup(skb);
125 /* GSO is not implemented for packets with an 802.1Q header, so
126 * we have to do segmentation before we add that header.
128 * GSO does work with hardware-accelerated VLAN tagging, but we
129 * can't use hardware-accelerated VLAN tagging since it
130 * requires the device to have a VLAN group configured (with
131 * e.g. vconfig(8)) and we don't do that.
133 * Having to do this here may be a performance loss, since we
134 * can't take advantage of TSO hardware support, although it
135 * does not make a measurable network performance difference
136 * for 1G Ethernet. Fixing that would require patching the
137 * kernel (either to add GSO support to the VLAN protocol or to
138 * support hardware-accelerated VLAN tagging without VLAN
139 * groups configured). */
140 if (skb_is_gso(skb)) {
141 struct sk_buff *segs;
143 segs = skb_gso_segment(skb, 0);
145 if (unlikely(IS_ERR(segs)))
146 return ERR_CAST(segs);
149 struct sk_buff *nskb = segs->next;
154 /* GSO can change the checksum type so update.*/
155 compute_ip_summed(segs, true);
157 segs = __vlan_put_tag(segs, tci);
160 struct odp_flow_key segkey = *key;
161 err = execute_actions(dp, segs,
168 while ((segs = nskb)) {
177 } while (segs->next);
180 compute_ip_summed(skb, true);
183 /* The hardware-accelerated version of vlan_put_tag() works
184 * only for a device that has a VLAN group configured (with
185 * e.g. vconfig(8)), so call the software-only version
186 * __vlan_put_tag() directly instead.
188 skb = __vlan_put_tag(skb, tci);
190 return ERR_PTR(-ENOMEM);
192 /* GSO doesn't fix up the hardware computed checksum so this
193 * will only be hit in the non-GSO case. */
194 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
195 skb->csum = csum_add(skb->csum, csum_partial(skb->data
196 + ETH_HLEN, VLAN_HLEN, 0));
202 static struct sk_buff *strip_vlan(struct sk_buff *skb,
203 struct odp_flow_key *key, gfp_t gfp)
205 skb = make_writable(skb, 0, gfp);
208 key->dl_vlan = htons(ODP_VLAN_NONE);
213 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
214 struct odp_flow_key *key,
215 const struct odp_action_dl_addr *a,
218 skb = make_writable(skb, 0, gfp);
220 struct ethhdr *eh = eth_hdr(skb);
221 if (a->type == ODPAT_SET_DL_SRC) {
222 memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
223 memcpy(key->dl_src, a->dl_addr, ETH_ALEN);
225 memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
226 memcpy(key->dl_dst, a->dl_addr, ETH_ALEN);
232 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
233 * covered by the sum has been changed from 'from' to 'to'. If set,
234 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
235 * Based on nf_proto_csum_replace4. */
236 static void update_csum(__sum16 *sum, struct sk_buff *skb,
237 __be32 from, __be32 to, int pseudohdr)
239 __be32 diff[] = { ~from, to };
241 if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
242 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
243 ~csum_unfold(*sum)));
244 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
245 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
247 } else if (pseudohdr)
248 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
252 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
253 struct odp_flow_key *key,
254 const struct odp_action_nw_addr *a,
257 if (key->dl_type != htons(ETH_P_IP))
260 skb = make_writable(skb, 0, gfp);
262 struct iphdr *nh = ip_hdr(skb);
263 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
265 u32 new = a->nw_addr;
267 if (key->nw_proto == IPPROTO_TCP) {
268 struct tcphdr *th = tcp_hdr(skb);
269 update_csum(&th->check, skb, old, new, 1);
270 } else if (key->nw_proto == IPPROTO_UDP) {
271 struct udphdr *th = udp_hdr(skb);
272 update_csum(&th->check, skb, old, new, 1);
274 update_csum(&nh->check, skb, old, new, 0);
277 if (a->type == ODPAT_SET_NW_SRC)
278 key->nw_src = a->nw_addr;
280 key->nw_dst = a->nw_addr;
285 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
286 struct odp_flow_key *key,
287 const struct odp_action_nw_tos *a,
290 if (key->dl_type != htons(ETH_P_IP))
293 skb = make_writable(skb, 0, gfp);
295 struct iphdr *nh = ip_hdr(skb);
300 /* Set the DSCP bits and preserve the ECN bits. */
301 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
302 update_csum(&nh->check, skb, htons((uint16_t)old),
303 htons((uint16_t)new), 0);
305 key->nw_tos = a->nw_tos;
310 static struct sk_buff *
311 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
312 const struct odp_action_tp_port *a,
317 if (key->dl_type != htons(ETH_P_IP))
320 if (key->nw_proto == IPPROTO_TCP)
321 check_ofs = offsetof(struct tcphdr, check);
322 else if (key->nw_proto == IPPROTO_UDP)
323 check_ofs = offsetof(struct udphdr, check);
327 skb = make_writable(skb, 0, gfp);
329 struct udphdr *th = udp_hdr(skb);
330 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
332 u16 new = a->tp_port;
333 update_csum((u16*)(skb_transport_header(skb) + check_ofs),
336 if (a->type == ODPAT_SET_TP_SRC)
337 key->tp_src = a->tp_port;
339 key->tp_dst = a->tp_port;
344 static inline unsigned packet_length(const struct sk_buff *skb)
346 unsigned length = skb->len - ETH_HLEN;
347 if (skb->protocol == htons(ETH_P_8021Q))
353 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
361 p = rcu_dereference(dp->ports[out_port]);
365 mtu = vport_get_mtu(p->vport);
366 if (packet_length(skb) > mtu && !skb_is_gso(skb)) {
367 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
368 dp_name(dp), packet_length(skb), mtu);
372 vport_send(p->vport, skb);
379 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
381 static int output_group(struct datapath *dp, __u16 group,
382 struct sk_buff *skb, gfp_t gfp)
384 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
390 for (i = 0; i < g->n_ports; i++) {
391 struct dp_port *p = rcu_dereference(dp->ports[g->ports[i]]);
392 if (!p || OVS_CB(skb)->dp_port == p)
394 if (prev_port != -1) {
395 struct sk_buff *clone = skb_clone(skb, gfp);
398 do_output(dp, clone, prev_port);
400 prev_port = p->port_no;
406 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
408 skb = skb_clone(skb, gfp);
411 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
414 /* Send a copy of this packet up to the sFlow agent, along with extra
415 * information about what happened to it. */
416 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
417 const union odp_action *a, int n_actions,
418 gfp_t gfp, struct dp_port *dp_port)
420 struct odp_sflow_sample_header *hdr;
421 unsigned int actlen = n_actions * sizeof(union odp_action);
422 unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
423 struct sk_buff *nskb;
425 nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
429 memcpy(__skb_push(nskb, actlen), a, actlen);
430 hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
431 hdr->n_actions = n_actions;
432 hdr->sample_pool = atomic_read(&dp_port->sflow_pool);
433 dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
436 /* Execute a list of actions against 'skb'. */
437 int execute_actions(struct datapath *dp, struct sk_buff *skb,
438 struct odp_flow_key *key,
439 const union odp_action *a, int n_actions,
442 /* Every output action needs a separate clone of 'skb', but the common
443 * case is just a single output action, so that doing a clone and
444 * then freeing the original skbuff is wasteful. So the following code
445 * is slightly obscure just to avoid that. */
449 if (dp->sflow_probability) {
450 struct dp_port *p = OVS_CB(skb)->dp_port;
452 atomic_inc(&p->sflow_pool);
453 if (dp->sflow_probability == UINT_MAX ||
454 net_random() < dp->sflow_probability)
455 sflow_sample(dp, skb, a, n_actions, gfp, p);
459 OVS_CB(skb)->tun_id = 0;
461 for (; n_actions > 0; a++, n_actions--) {
462 WARN_ON_ONCE(skb_shared(skb));
463 if (prev_port != -1) {
464 do_output(dp, skb_clone(skb, gfp), prev_port);
470 prev_port = a->output.port;
473 case ODPAT_OUTPUT_GROUP:
474 prev_port = output_group(dp, a->output_group.group,
478 case ODPAT_CONTROLLER:
479 err = output_control(dp, skb, a->controller.arg, gfp);
486 case ODPAT_SET_TUNNEL:
487 set_tunnel(skb, key, a->tunnel.tun_id);
490 case ODPAT_SET_VLAN_VID:
491 case ODPAT_SET_VLAN_PCP:
492 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
497 case ODPAT_STRIP_VLAN:
498 skb = strip_vlan(skb, key, gfp);
501 case ODPAT_SET_DL_SRC:
502 case ODPAT_SET_DL_DST:
503 skb = set_dl_addr(skb, key, &a->dl_addr, gfp);
506 case ODPAT_SET_NW_SRC:
507 case ODPAT_SET_NW_DST:
508 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
511 case ODPAT_SET_NW_TOS:
512 skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
515 case ODPAT_SET_TP_SRC:
516 case ODPAT_SET_TP_DST:
517 skb = set_tp_port(skb, key, &a->tp_port, gfp);
524 do_output(dp, skb, prev_port);