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/xflow.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 xflow_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 xflow_key *key, const union xflow_action *a,
84 int n_actions, gfp_t gfp)
86 __be16 mask = a->dl_tci.mask;
87 __be16 tci = a->dl_tci.tci;
89 key->dl_tci = (key->dl_tci & ~(mask | VLAN_TAG_PRESENT)) | tci;
91 skb = make_writable(skb, VLAN_HLEN, gfp);
93 return ERR_PTR(-ENOMEM);
95 if (skb->protocol == htons(ETH_P_8021Q)) {
96 /* Modify vlan id, but maintain other TCI values */
97 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
98 __be16 old_tci = vh->h_vlan_TCI;
100 vh->h_vlan_TCI = (vh->h_vlan_TCI & ~mask) | tci;
102 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE) {
103 __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
105 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
109 /* Add vlan header */
111 /* Set up checksumming pointers for checksum-deferred packets
112 * on Xen. Otherwise, dev_queue_xmit() will try to do this
113 * when we send the packet out on the wire, and it will fail at
114 * that point because skb_checksum_setup() will not look inside
115 * an 802.1Q header. */
116 vswitch_skb_checksum_setup(skb);
118 /* GSO is not implemented for packets with an 802.1Q header, so
119 * we have to do segmentation before we add that header.
121 * GSO does work with hardware-accelerated VLAN tagging, but we
122 * can't use hardware-accelerated VLAN tagging since it
123 * requires the device to have a VLAN group configured (with
124 * e.g. vconfig(8)) and we don't do that.
126 * Having to do this here may be a performance loss, since we
127 * can't take advantage of TSO hardware support, although it
128 * does not make a measurable network performance difference
129 * for 1G Ethernet. Fixing that would require patching the
130 * kernel (either to add GSO support to the VLAN protocol or to
131 * support hardware-accelerated VLAN tagging without VLAN
132 * groups configured). */
133 if (skb_is_gso(skb)) {
134 struct sk_buff *segs;
136 segs = skb_gso_segment(skb, 0);
138 if (unlikely(IS_ERR(segs)))
139 return ERR_CAST(segs);
142 struct sk_buff *nskb = segs->next;
147 /* GSO can change the checksum type so update.*/
148 compute_ip_summed(segs, true);
150 segs = __vlan_put_tag(segs, ntohs(tci));
153 struct xflow_key segkey = *key;
154 err = execute_actions(dp, segs,
161 while ((segs = nskb)) {
170 } while (segs->next);
173 compute_ip_summed(skb, true);
176 /* The hardware-accelerated version of vlan_put_tag() works
177 * only for a device that has a VLAN group configured (with
178 * e.g. vconfig(8)), so call the software-only version
179 * __vlan_put_tag() directly instead.
181 skb = __vlan_put_tag(skb, ntohs(tci));
183 return ERR_PTR(-ENOMEM);
185 /* GSO doesn't fix up the hardware computed checksum so this
186 * will only be hit in the non-GSO case. */
187 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
188 skb->csum = csum_add(skb->csum, csum_partial(skb->data
189 + ETH_HLEN, VLAN_HLEN, 0));
195 static struct sk_buff *strip_vlan(struct sk_buff *skb,
196 struct xflow_key *key, gfp_t gfp)
198 skb = make_writable(skb, 0, gfp);
201 key->dl_tci = htons(0);
206 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
207 struct xflow_key *key,
208 const struct xflow_action_dl_addr *a,
211 skb = make_writable(skb, 0, gfp);
213 struct ethhdr *eh = eth_hdr(skb);
214 if (a->type == XFLOWAT_SET_DL_SRC) {
215 memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
216 memcpy(key->dl_src, a->dl_addr, ETH_ALEN);
218 memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
219 memcpy(key->dl_dst, a->dl_addr, ETH_ALEN);
225 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
226 * covered by the sum has been changed from 'from' to 'to'. If set,
227 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
228 * Based on nf_proto_csum_replace4. */
229 static void update_csum(__sum16 *sum, struct sk_buff *skb,
230 __be32 from, __be32 to, int pseudohdr)
232 __be32 diff[] = { ~from, to };
234 if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
235 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
236 ~csum_unfold(*sum)));
237 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
238 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
240 } else if (pseudohdr)
241 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
245 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
246 struct xflow_key *key,
247 const struct xflow_action_nw_addr *a,
250 if (key->dl_type != htons(ETH_P_IP))
253 skb = make_writable(skb, 0, gfp);
255 struct iphdr *nh = ip_hdr(skb);
256 u32 *f = a->type == XFLOWAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
258 u32 new = a->nw_addr;
260 if (key->nw_proto == IPPROTO_TCP) {
261 struct tcphdr *th = tcp_hdr(skb);
262 update_csum(&th->check, skb, old, new, 1);
263 } else if (key->nw_proto == IPPROTO_UDP) {
264 struct udphdr *th = udp_hdr(skb);
265 update_csum(&th->check, skb, old, new, 1);
267 update_csum(&nh->check, skb, old, new, 0);
270 if (a->type == XFLOWAT_SET_NW_SRC)
271 key->nw_src = a->nw_addr;
273 key->nw_dst = a->nw_addr;
278 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
279 struct xflow_key *key,
280 const struct xflow_action_nw_tos *a,
283 if (key->dl_type != htons(ETH_P_IP))
286 skb = make_writable(skb, 0, gfp);
288 struct iphdr *nh = ip_hdr(skb);
293 /* Set the DSCP bits and preserve the ECN bits. */
294 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
295 update_csum(&nh->check, skb, htons((uint16_t)old),
296 htons((uint16_t)new), 0);
298 key->nw_tos = a->nw_tos;
303 static struct sk_buff *
304 set_tp_port(struct sk_buff *skb, struct xflow_key *key,
305 const struct xflow_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 == XFLOWAT_SET_TP_SRC ? &th->source : &th->dest;
325 u16 new = a->tp_port;
326 update_csum((u16*)(skb_transport_header(skb) + check_ofs),
329 if (a->type == XFLOWAT_SET_TP_SRC)
330 key->tp_src = a->tp_port;
332 key->tp_dst = a->tp_port;
337 static inline unsigned packet_length(const struct sk_buff *skb)
339 unsigned length = skb->len - ETH_HLEN;
340 if (skb->protocol == htons(ETH_P_8021Q))
346 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
354 p = rcu_dereference(dp->ports[out_port]);
358 mtu = vport_get_mtu(p->vport);
359 if (packet_length(skb) > mtu && !skb_is_gso(skb)) {
360 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
361 dp_name(dp), packet_length(skb), mtu);
365 vport_send(p->vport, skb);
372 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
374 static int output_group(struct datapath *dp, __u16 group,
375 struct sk_buff *skb, gfp_t gfp)
377 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
383 for (i = 0; i < g->n_ports; i++) {
384 struct dp_port *p = rcu_dereference(dp->ports[g->ports[i]]);
385 if (!p || OVS_CB(skb)->dp_port == p)
387 if (prev_port != -1) {
388 struct sk_buff *clone = skb_clone(skb, gfp);
391 do_output(dp, clone, prev_port);
393 prev_port = p->port_no;
399 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
401 skb = skb_clone(skb, gfp);
404 return dp_output_control(dp, skb, _XFLOWL_ACTION_NR, arg);
407 /* Send a copy of this packet up to the sFlow agent, along with extra
408 * information about what happened to it. */
409 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
410 const union xflow_action *a, int n_actions,
411 gfp_t gfp, struct dp_port *dp_port)
413 struct xflow_sflow_sample_header *hdr;
414 unsigned int actlen = n_actions * sizeof(union xflow_action);
415 unsigned int hdrlen = sizeof(struct xflow_sflow_sample_header);
416 struct sk_buff *nskb;
418 nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
422 memcpy(__skb_push(nskb, actlen), a, actlen);
423 hdr = (struct xflow_sflow_sample_header*)__skb_push(nskb, hdrlen);
424 hdr->n_actions = n_actions;
425 hdr->sample_pool = atomic_read(&dp_port->sflow_pool);
426 dp_output_control(dp, nskb, _XFLOWL_SFLOW_NR, 0);
429 /* Execute a list of actions against 'skb'. */
430 int execute_actions(struct datapath *dp, struct sk_buff *skb,
431 struct xflow_key *key,
432 const union xflow_action *a, int n_actions,
435 /* Every output action needs a separate clone of 'skb', but the common
436 * case is just a single output action, so that doing a clone and
437 * then freeing the original skbuff is wasteful. So the following code
438 * is slightly obscure just to avoid that. */
442 if (dp->sflow_probability) {
443 struct dp_port *p = OVS_CB(skb)->dp_port;
445 atomic_inc(&p->sflow_pool);
446 if (dp->sflow_probability == UINT_MAX ||
447 net_random() < dp->sflow_probability)
448 sflow_sample(dp, skb, a, n_actions, gfp, p);
452 OVS_CB(skb)->tun_id = 0;
454 for (; n_actions > 0; a++, n_actions--) {
455 WARN_ON_ONCE(skb_shared(skb));
456 if (prev_port != -1) {
457 do_output(dp, skb_clone(skb, gfp), prev_port);
463 prev_port = a->output.port;
466 case XFLOWAT_OUTPUT_GROUP:
467 prev_port = output_group(dp, a->output_group.group,
471 case XFLOWAT_CONTROLLER:
472 err = output_control(dp, skb, a->controller.arg, gfp);
479 case XFLOWAT_SET_TUNNEL:
480 set_tunnel(skb, key, a->tunnel.tun_id);
483 case XFLOWAT_SET_DL_TCI:
484 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
489 case XFLOWAT_STRIP_VLAN:
490 skb = strip_vlan(skb, key, gfp);
493 case XFLOWAT_SET_DL_SRC:
494 case XFLOWAT_SET_DL_DST:
495 skb = set_dl_addr(skb, key, &a->dl_addr, gfp);
498 case XFLOWAT_SET_NW_SRC:
499 case XFLOWAT_SET_NW_DST:
500 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
503 case XFLOWAT_SET_NW_TOS:
504 skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
507 case XFLOWAT_SET_TP_SRC:
508 case XFLOWAT_SET_TP_DST:
509 skb = set_tp_port(skb, key, &a->tp_port, gfp);
516 do_output(dp, skb, prev_port);