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);
35 unsigned int hdr_len = (skb_transport_offset(skb)
36 + sizeof(struct tcphdr));
37 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
45 static struct sk_buff *
46 vlan_pull_tag(struct sk_buff *skb)
48 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
52 /* Verify we were given a vlan packet */
53 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
56 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
58 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
60 skb->protocol = eh->h_proto;
61 skb->mac_header += VLAN_HLEN;
67 static struct sk_buff *
68 modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
69 struct odp_flow_key *key, const union odp_action *a,
70 int n_actions, gfp_t gfp)
74 if (a->type == ODPAT_SET_VLAN_VID) {
75 tci = ntohs(a->vlan_vid.vlan_vid);
77 key->dl_vlan = htons(tci & mask);
79 tci = a->vlan_pcp.vlan_pcp << 13;
83 skb = make_writable(skb, gfp);
85 return ERR_PTR(-ENOMEM);
87 if (skb->protocol == htons(ETH_P_8021Q)) {
88 /* Modify vlan id, but maintain other TCI values */
89 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
90 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
94 /* Set up checksumming pointers for checksum-deferred packets
95 * on Xen. Otherwise, dev_queue_xmit() will try to do this
96 * when we send the packet out on the wire, and it will fail at
97 * that point because skb_checksum_setup() will not look inside
98 * an 802.1Q header. */
99 skb_checksum_setup(skb);
101 /* GSO is not implemented for packets with an 802.1Q header, so
102 * we have to do segmentation before we add that header.
104 * GSO does work with hardware-accelerated VLAN tagging, but we
105 * can't use hardware-accelerated VLAN tagging since it
106 * requires the device to have a VLAN group configured (with
107 * e.g. vconfig(8)) and we don't do that.
109 * Having to do this here may be a performance loss, since we
110 * can't take advantage of TSO hardware support, although it
111 * does not make a measurable network performance difference
112 * for 1G Ethernet. Fixing that would require patching the
113 * kernel (either to add GSO support to the VLAN protocol or to
114 * support hardware-accelerated VLAN tagging without VLAN
115 * groups configured). */
116 if (skb_is_gso(skb)) {
117 struct sk_buff *segs;
119 segs = skb_gso_segment(skb, 0);
121 if (unlikely(IS_ERR(segs)))
122 return ERR_CAST(segs);
125 struct sk_buff *nskb = segs->next;
130 segs = __vlan_put_tag(segs, tci);
133 struct odp_flow_key segkey = *key;
134 err = execute_actions(dp, segs,
141 while ((segs = nskb)) {
150 } while (segs->next);
155 /* The hardware-accelerated version of vlan_put_tag() works
156 * only for a device that has a VLAN group configured (with
157 * e.g. vconfig(8)), so call the software-only version
158 * __vlan_put_tag() directly instead.
160 skb = __vlan_put_tag(skb, tci);
162 return ERR_PTR(-ENOMEM);
168 static struct sk_buff *strip_vlan(struct sk_buff *skb,
169 struct odp_flow_key *key, gfp_t gfp)
171 skb = make_writable(skb, gfp);
174 key->dl_vlan = htons(ODP_VLAN_NONE);
179 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
180 const struct odp_action_dl_addr *a,
183 skb = make_writable(skb, gfp);
185 struct ethhdr *eh = eth_hdr(skb);
186 memcpy(a->type == ODPAT_SET_DL_SRC ? eh->h_source : eh->h_dest,
187 a->dl_addr, ETH_ALEN);
192 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
193 * covered by the sum has been changed from 'from' to 'to'. If set,
194 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
195 * Based on nf_proto_csum_replace4. */
196 static void update_csum(__sum16 *sum, struct sk_buff *skb,
197 __be32 from, __be32 to, int pseudohdr)
199 __be32 diff[] = { ~from, to };
200 if (skb->ip_summed != CHECKSUM_PARTIAL) {
201 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
202 ~csum_unfold(*sum)));
203 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
204 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
206 } else if (pseudohdr)
207 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
211 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
212 struct odp_flow_key *key,
213 const struct odp_action_nw_addr *a,
216 if (key->dl_type != htons(ETH_P_IP))
219 skb = make_writable(skb, gfp);
221 struct iphdr *nh = ip_hdr(skb);
222 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
224 u32 new = a->nw_addr;
226 if (key->nw_proto == IPPROTO_TCP) {
227 struct tcphdr *th = tcp_hdr(skb);
228 update_csum(&th->check, skb, old, new, 1);
229 } else if (key->nw_proto == IPPROTO_UDP) {
230 struct udphdr *th = udp_hdr(skb);
231 update_csum(&th->check, skb, old, new, 1);
233 update_csum(&nh->check, skb, old, new, 0);
239 static struct sk_buff *
240 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
241 const struct odp_action_tp_port *a,
246 if (key->dl_type != htons(ETH_P_IP))
249 if (key->nw_proto == IPPROTO_TCP)
250 check_ofs = offsetof(struct tcphdr, check);
251 else if (key->nw_proto == IPPROTO_UDP)
252 check_ofs = offsetof(struct udphdr, check);
256 skb = make_writable(skb, gfp);
258 struct udphdr *th = udp_hdr(skb);
259 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
261 u16 new = a->tp_port;
262 update_csum((u16*)((u8*)skb->data + check_ofs),
269 static inline unsigned packet_length(const struct sk_buff *skb)
271 unsigned length = skb->len - ETH_HLEN;
272 if (skb->protocol == htons(ETH_P_8021Q))
277 int dp_xmit_skb(struct sk_buff *skb)
279 struct datapath *dp = skb->dev->br_port->dp;
282 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) {
283 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
284 dp_name(dp), packet_length(skb), skb->dev->mtu);
295 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
297 struct net_bridge_port *p;
298 struct net_device *dev;
303 p = dp->ports[out_port];
307 dev = skb->dev = p->dev;
309 dp_dev_recv(dev, skb);
318 /* Never consumes 'skb'. Returns a port that 'skb' should be sent to, -1 if
320 static int output_group(struct datapath *dp, __u16 group,
321 struct sk_buff *skb, gfp_t gfp)
323 struct dp_port_group *g = rcu_dereference(dp->groups[group]);
329 for (i = 0; i < g->n_ports; i++) {
330 struct net_bridge_port *p = dp->ports[g->ports[i]];
331 if (!p || skb->dev == p->dev)
333 if (prev_port != -1) {
334 struct sk_buff *clone = skb_clone(skb, gfp);
337 do_output(dp, clone, prev_port);
339 prev_port = p->port_no;
345 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
347 skb = skb_clone(skb, gfp);
350 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
353 /* Execute a list of actions against 'skb'. */
354 int execute_actions(struct datapath *dp, struct sk_buff *skb,
355 struct odp_flow_key *key,
356 const union odp_action *a, int n_actions,
359 /* Every output action needs a separate clone of 'skb', but the common
360 * case is just a single output action, so that doing a clone and
361 * then freeing the original skbuff is wasteful. So the following code
362 * is slightly obscure just to avoid that. */
365 for (; n_actions > 0; a++, n_actions--) {
366 WARN_ON_ONCE(skb_shared(skb));
367 if (prev_port != -1) {
368 do_output(dp, skb_clone(skb, gfp), prev_port);
374 prev_port = a->output.port;
377 case ODPAT_OUTPUT_GROUP:
378 prev_port = output_group(dp, a->output_group.group,
382 case ODPAT_CONTROLLER:
383 err = output_control(dp, skb, a->controller.arg, gfp);
390 case ODPAT_SET_VLAN_VID:
391 case ODPAT_SET_VLAN_PCP:
392 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
397 case ODPAT_STRIP_VLAN:
398 skb = strip_vlan(skb, key, gfp);
401 case ODPAT_SET_DL_SRC:
402 case ODPAT_SET_DL_DST:
403 skb = set_dl_addr(skb, &a->dl_addr, gfp);
406 case ODPAT_SET_NW_SRC:
407 case ODPAT_SET_NW_DST:
408 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
411 case ODPAT_SET_TP_SRC:
412 case ODPAT_SET_TP_DST:
413 skb = set_tp_port(skb, key, &a->tp_port, gfp);
420 do_output(dp, skb, prev_port);