2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009 Nicira Networks.
6 /* Functions for executing flow actions. */
8 #include <linux/skbuff.h>
11 #include <linux/tcp.h>
12 #include <linux/udp.h>
13 #include <linux/in6.h>
14 #include <linux/if_vlan.h>
16 #include <net/checksum.h>
20 #include "openvswitch/datapath-protocol.h"
23 make_writable(struct sk_buff *skb, gfp_t gfp)
25 if (skb_shared(skb) || skb_cloned(skb)) {
26 struct sk_buff *nskb = skb_copy(skb, gfp);
32 unsigned int hdr_len = (skb_transport_offset(skb)
33 + sizeof(struct tcphdr));
34 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
42 static struct sk_buff *
43 vlan_pull_tag(struct sk_buff *skb)
45 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
49 /* Verify we were given a vlan packet */
50 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
53 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
55 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
57 skb->protocol = eh->h_proto;
58 skb->mac_header += VLAN_HLEN;
64 static struct sk_buff *
65 modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
66 struct odp_flow_key *key, const union odp_action *a,
67 int n_actions, gfp_t gfp)
71 if (a->type == ODPAT_SET_VLAN_VID) {
72 tci = ntohs(a->vlan_vid.vlan_vid);
74 key->dl_vlan = htons(tci & mask);
76 tci = a->vlan_pcp.vlan_pcp << 13;
80 skb = make_writable(skb, gfp);
82 return ERR_PTR(-ENOMEM);
84 if (skb->protocol == htons(ETH_P_8021Q)) {
85 /* Modify vlan id, but maintain other TCI values */
86 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
87 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
91 /* Set up checksumming pointers for checksum-deferred packets
92 * on Xen. Otherwise, dev_queue_xmit() will try to do this
93 * when we send the packet out on the wire, and it will fail at
94 * that point because skb_checksum_setup() will not look inside
95 * an 802.1Q header. */
96 skb_checksum_setup(skb);
98 /* GSO is not implemented for packets with an 802.1Q header, so
99 * we have to do segmentation before we add that header.
101 * GSO does work with hardware-accelerated VLAN tagging, but we
102 * can't use hardware-accelerated VLAN tagging since it
103 * requires the device to have a VLAN group configured (with
104 * e.g. vconfig(8)) and we don't do that.
106 * Having to do this here may be a performance loss, since we
107 * can't take advantage of TSO hardware support, although it
108 * does not make a measurable network performance difference
109 * for 1G Ethernet. Fixing that would require patching the
110 * kernel (either to add GSO support to the VLAN protocol or to
111 * support hardware-accelerated VLAN tagging without VLAN
112 * groups configured). */
113 if (skb_is_gso(skb)) {
114 struct sk_buff *segs;
116 segs = skb_gso_segment(skb, 0);
118 if (unlikely(IS_ERR(segs)))
119 return ERR_CAST(segs);
122 struct sk_buff *nskb = segs->next;
127 segs = __vlan_put_tag(segs, tci);
130 struct odp_flow_key segkey = *key;
131 err = execute_actions(dp, segs,
138 while ((segs = nskb)) {
147 } while (segs->next);
152 /* The hardware-accelerated version of vlan_put_tag() works
153 * only for a device that has a VLAN group configured (with
154 * e.g. vconfig(8)), so call the software-only version
155 * __vlan_put_tag() directly instead.
157 skb = __vlan_put_tag(skb, tci);
159 return ERR_PTR(-ENOMEM);
165 static struct sk_buff *strip_vlan(struct sk_buff *skb,
166 struct odp_flow_key *key, gfp_t gfp)
168 skb = make_writable(skb, gfp);
171 key->dl_vlan = htons(ODP_VLAN_NONE);
176 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
177 const struct odp_action_dl_addr *a,
180 skb = make_writable(skb, gfp);
182 struct ethhdr *eh = eth_hdr(skb);
183 memcpy(a->type == ODPAT_SET_DL_SRC ? eh->h_source : eh->h_dest,
184 a->dl_addr, ETH_ALEN);
189 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
190 * covered by the sum has been changed from 'from' to 'to'. If set,
191 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
192 * Based on nf_proto_csum_replace4. */
193 static void update_csum(__sum16 *sum, struct sk_buff *skb,
194 __be32 from, __be32 to, int pseudohdr)
196 __be32 diff[] = { ~from, to };
197 if (skb->ip_summed != CHECKSUM_PARTIAL) {
198 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
199 ~csum_unfold(*sum)));
200 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
201 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
203 } else if (pseudohdr)
204 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
208 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
209 struct odp_flow_key *key,
210 const struct odp_action_nw_addr *a,
213 if (key->dl_type != htons(ETH_P_IP))
216 skb = make_writable(skb, gfp);
218 struct iphdr *nh = ip_hdr(skb);
219 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
221 u32 new = a->nw_addr;
223 if (key->nw_proto == IPPROTO_TCP) {
224 struct tcphdr *th = tcp_hdr(skb);
225 update_csum(&th->check, skb, old, new, 1);
226 } else if (key->nw_proto == IPPROTO_UDP) {
227 struct udphdr *th = udp_hdr(skb);
228 update_csum(&th->check, skb, old, new, 1);
230 update_csum(&nh->check, skb, old, new, 0);
236 static struct sk_buff *
237 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
238 const struct odp_action_tp_port *a,
243 if (key->dl_type != htons(ETH_P_IP))
246 if (key->nw_proto == IPPROTO_TCP)
247 check_ofs = offsetof(struct tcphdr, check);
248 else if (key->nw_proto == IPPROTO_UDP)
249 check_ofs = offsetof(struct udphdr, check);
253 skb = make_writable(skb, gfp);
255 struct udphdr *th = udp_hdr(skb);
256 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
258 u16 new = a->tp_port;
259 update_csum((u16*)((u8*)skb->data + check_ofs),
266 static inline unsigned packet_length(const struct sk_buff *skb)
268 unsigned length = skb->len - ETH_HLEN;
269 if (skb->protocol == htons(ETH_P_8021Q))
274 int dp_xmit_skb(struct sk_buff *skb)
276 struct datapath *dp = skb->dev->br_port->dp;
279 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) {
280 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
281 dp_name(dp), packet_length(skb), skb->dev->mtu);
292 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
294 struct net_bridge_port *p;
295 struct net_device *dev;
300 p = dp->ports[out_port];
304 dev = skb->dev = p->dev;
306 dp_dev_recv(dev, skb);
315 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
317 static int output_group(struct datapath *dp, __u16 group,
318 struct sk_buff *skb, gfp_t gfp)
320 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
326 for (i = 0; i < g->n_ports; i++) {
327 struct net_bridge_port *p = dp->ports[g->ports[i]];
328 if (!p || skb->dev == p->dev)
330 if (prev_port != -1) {
331 struct sk_buff *clone = skb_clone(skb, gfp);
334 do_output(dp, clone, prev_port);
336 prev_port = p->port_no;
342 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
344 skb = skb_clone(skb, gfp);
347 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
350 /* Execute a list of actions against 'skb'. */
351 int execute_actions(struct datapath *dp, struct sk_buff *skb,
352 struct odp_flow_key *key,
353 const union odp_action *a, int n_actions,
356 /* Every output action needs a separate clone of 'skb', but the common
357 * case is just a single output action, so that doing a clone and
358 * then freeing the original skbuff is wasteful. So the following code
359 * is slightly obscure just to avoid that. */
362 for (; n_actions > 0; a++, n_actions--) {
363 WARN_ON_ONCE(skb_shared(skb));
364 if (prev_port != -1) {
365 do_output(dp, skb_clone(skb, gfp), prev_port);
371 prev_port = a->output.port;
374 case ODPAT_OUTPUT_GROUP:
375 prev_port = output_group(dp, a->output_group.group,
379 case ODPAT_CONTROLLER:
380 err = output_control(dp, skb, a->controller.arg, gfp);
387 case ODPAT_SET_VLAN_VID:
388 case ODPAT_SET_VLAN_PCP:
389 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
394 case ODPAT_STRIP_VLAN:
395 skb = strip_vlan(skb, key, gfp);
398 case ODPAT_SET_DL_SRC:
399 case ODPAT_SET_DL_DST:
400 skb = set_dl_addr(skb, &a->dl_addr, gfp);
403 case ODPAT_SET_NW_SRC:
404 case ODPAT_SET_NW_DST:
405 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
408 case ODPAT_SET_TP_SRC:
409 case ODPAT_SET_TP_DST:
410 skb = set_tp_port(skb, key, &a->tp_port, gfp);
417 do_output(dp, skb, prev_port);