2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009 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>
19 #include <net/checksum.h>
23 #include "openvswitch/datapath-protocol.h"
26 make_writable(struct sk_buff *skb, gfp_t gfp)
28 if (skb_shared(skb) || skb_cloned(skb)) {
29 struct sk_buff *nskb = skb_copy(skb, gfp);
31 #if defined(CONFIG_XEN) && LINUX_VERSION_CODE == KERNEL_VERSION(2,6,18)
32 /* These fields are copied in skb_clone but not in
33 * skb_copy or related functions. We need to manually
34 * copy them over here. */
35 nskb->proto_data_valid = skb->proto_data_valid;
36 nskb->proto_csum_blank = skb->proto_csum_blank;
42 unsigned int hdr_len = (skb_transport_offset(skb)
43 + sizeof(struct tcphdr));
44 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
52 static struct sk_buff *
53 vlan_pull_tag(struct sk_buff *skb)
55 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
59 /* Verify we were given a vlan packet */
60 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
63 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
65 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
67 skb->protocol = eh->h_proto;
68 skb->mac_header += VLAN_HLEN;
74 static struct sk_buff *
75 modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
76 struct odp_flow_key *key, const union odp_action *a,
77 int n_actions, gfp_t gfp)
81 if (a->type == ODPAT_SET_VLAN_VID) {
82 tci = ntohs(a->vlan_vid.vlan_vid);
84 key->dl_vlan = htons(tci & mask);
86 tci = a->vlan_pcp.vlan_pcp << 13;
90 skb = make_writable(skb, gfp);
92 return ERR_PTR(-ENOMEM);
94 if (skb->protocol == htons(ETH_P_8021Q)) {
95 /* Modify vlan id, but maintain other TCI values */
96 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
97 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
101 /* Set up checksumming pointers for checksum-deferred packets
102 * on Xen. Otherwise, dev_queue_xmit() will try to do this
103 * when we send the packet out on the wire, and it will fail at
104 * that point because skb_checksum_setup() will not look inside
105 * an 802.1Q header. */
106 skb_checksum_setup(skb);
108 /* GSO is not implemented for packets with an 802.1Q header, so
109 * we have to do segmentation before we add that header.
111 * GSO does work with hardware-accelerated VLAN tagging, but we
112 * can't use hardware-accelerated VLAN tagging since it
113 * requires the device to have a VLAN group configured (with
114 * e.g. vconfig(8)) and we don't do that.
116 * Having to do this here may be a performance loss, since we
117 * can't take advantage of TSO hardware support, although it
118 * does not make a measurable network performance difference
119 * for 1G Ethernet. Fixing that would require patching the
120 * kernel (either to add GSO support to the VLAN protocol or to
121 * support hardware-accelerated VLAN tagging without VLAN
122 * groups configured). */
123 if (skb_is_gso(skb)) {
124 struct sk_buff *segs;
126 segs = skb_gso_segment(skb, 0);
128 if (unlikely(IS_ERR(segs)))
129 return ERR_CAST(segs);
132 struct sk_buff *nskb = segs->next;
137 segs = __vlan_put_tag(segs, tci);
140 struct odp_flow_key segkey = *key;
141 err = execute_actions(dp, segs,
148 while ((segs = nskb)) {
157 } while (segs->next);
162 /* The hardware-accelerated version of vlan_put_tag() works
163 * only for a device that has a VLAN group configured (with
164 * e.g. vconfig(8)), so call the software-only version
165 * __vlan_put_tag() directly instead.
167 skb = __vlan_put_tag(skb, tci);
169 return ERR_PTR(-ENOMEM);
175 static struct sk_buff *strip_vlan(struct sk_buff *skb,
176 struct odp_flow_key *key, gfp_t gfp)
178 skb = make_writable(skb, gfp);
181 key->dl_vlan = htons(ODP_VLAN_NONE);
186 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
187 const struct odp_action_dl_addr *a,
190 skb = make_writable(skb, gfp);
192 struct ethhdr *eh = eth_hdr(skb);
193 memcpy(a->type == ODPAT_SET_DL_SRC ? eh->h_source : eh->h_dest,
194 a->dl_addr, ETH_ALEN);
199 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
200 * covered by the sum has been changed from 'from' to 'to'. If set,
201 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
202 * Based on nf_proto_csum_replace4. */
203 static void update_csum(__sum16 *sum, struct sk_buff *skb,
204 __be32 from, __be32 to, int pseudohdr)
206 __be32 diff[] = { ~from, to };
207 if (skb->ip_summed != CHECKSUM_PARTIAL) {
208 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
209 ~csum_unfold(*sum)));
210 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
211 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
213 } else if (pseudohdr)
214 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
218 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
219 struct odp_flow_key *key,
220 const struct odp_action_nw_addr *a,
223 if (key->dl_type != htons(ETH_P_IP))
226 skb = make_writable(skb, gfp);
228 struct iphdr *nh = ip_hdr(skb);
229 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
231 u32 new = a->nw_addr;
233 if (key->nw_proto == IPPROTO_TCP) {
234 struct tcphdr *th = tcp_hdr(skb);
235 update_csum(&th->check, skb, old, new, 1);
236 } else if (key->nw_proto == IPPROTO_UDP) {
237 struct udphdr *th = udp_hdr(skb);
238 update_csum(&th->check, skb, old, new, 1);
240 update_csum(&nh->check, skb, old, new, 0);
246 static struct sk_buff *
247 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
248 const struct odp_action_tp_port *a,
253 if (key->dl_type != htons(ETH_P_IP))
256 if (key->nw_proto == IPPROTO_TCP)
257 check_ofs = offsetof(struct tcphdr, check);
258 else if (key->nw_proto == IPPROTO_UDP)
259 check_ofs = offsetof(struct udphdr, check);
263 skb = make_writable(skb, gfp);
265 struct udphdr *th = udp_hdr(skb);
266 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
268 u16 new = a->tp_port;
269 update_csum((u16*)((u8*)skb->data + check_ofs),
276 static inline unsigned packet_length(const struct sk_buff *skb)
278 unsigned length = skb->len - ETH_HLEN;
279 if (skb->protocol == htons(ETH_P_8021Q))
284 int dp_xmit_skb(struct sk_buff *skb)
286 struct datapath *dp = skb->dev->br_port->dp;
289 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) {
290 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
291 dp_name(dp), packet_length(skb), skb->dev->mtu);
302 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
304 struct net_bridge_port *p;
305 struct net_device *dev;
310 p = dp->ports[out_port];
314 dev = skb->dev = p->dev;
316 dp_dev_recv(dev, skb);
325 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
327 static int output_group(struct datapath *dp, __u16 group,
328 struct sk_buff *skb, gfp_t gfp)
330 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
336 for (i = 0; i < g->n_ports; i++) {
337 struct net_bridge_port *p = dp->ports[g->ports[i]];
338 if (!p || skb->dev == p->dev)
340 if (prev_port != -1) {
341 struct sk_buff *clone = skb_clone(skb, gfp);
344 do_output(dp, clone, prev_port);
346 prev_port = p->port_no;
352 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
354 skb = skb_clone(skb, gfp);
357 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
360 /* Execute a list of actions against 'skb'. */
361 int execute_actions(struct datapath *dp, struct sk_buff *skb,
362 struct odp_flow_key *key,
363 const union odp_action *a, int n_actions,
366 /* Every output action needs a separate clone of 'skb', but the common
367 * case is just a single output action, so that doing a clone and
368 * then freeing the original skbuff is wasteful. So the following code
369 * is slightly obscure just to avoid that. */
372 for (; n_actions > 0; a++, n_actions--) {
373 WARN_ON_ONCE(skb_shared(skb));
374 if (prev_port != -1) {
375 do_output(dp, skb_clone(skb, gfp), prev_port);
381 prev_port = a->output.port;
384 case ODPAT_OUTPUT_GROUP:
385 prev_port = output_group(dp, a->output_group.group,
389 case ODPAT_CONTROLLER:
390 err = output_control(dp, skb, a->controller.arg, gfp);
397 case ODPAT_SET_VLAN_VID:
398 case ODPAT_SET_VLAN_PCP:
399 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
404 case ODPAT_STRIP_VLAN:
405 skb = strip_vlan(skb, key, gfp);
408 case ODPAT_SET_DL_SRC:
409 case ODPAT_SET_DL_DST:
410 skb = set_dl_addr(skb, &a->dl_addr, gfp);
413 case ODPAT_SET_NW_SRC:
414 case ODPAT_SET_NW_DST:
415 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
418 case ODPAT_SET_TP_SRC:
419 case ODPAT_SET_TP_DST:
420 skb = set_tp_port(skb, key, &a->tp_port, gfp);
427 do_output(dp, skb, prev_port);