2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009, 2010, 2011 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_arp.h>
18 #include <linux/if_vlan.h>
19 #include <net/inet_ecn.h>
21 #include <net/checksum.h>
26 #include "loop_counter.h"
27 #include "openvswitch/datapath-protocol.h"
31 static int do_execute_actions(struct datapath *, struct sk_buff *,
32 struct sw_flow_actions *acts);
34 static int make_writable(struct sk_buff *skb, int write_len)
36 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
39 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
42 static int strip_vlan(struct sk_buff *skb)
47 if (vlan_tx_tag_present(skb)) {
52 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
53 skb->len < VLAN_ETH_HLEN))
56 err = make_writable(skb, VLAN_ETH_HLEN);
60 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
61 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
62 + ETH_HLEN, VLAN_HLEN, 0));
64 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
66 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
68 skb->protocol = eh->h_proto;
69 skb->mac_header += VLAN_HLEN;
74 static int modify_vlan_tci(struct sk_buff *skb, __be16 tci)
76 if (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_8021Q)) {
79 if (unlikely(skb->len < VLAN_ETH_HLEN))
82 err = strip_vlan(skb);
87 __vlan_hwaccel_put_tag(skb, ntohs(tci));
92 static bool is_ip(struct sk_buff *skb)
94 return (OVS_CB(skb)->flow->key.eth.type == htons(ETH_P_IP) &&
95 skb->transport_header > skb->network_header);
98 static __sum16 *get_l4_checksum(struct sk_buff *skb)
100 u8 nw_proto = OVS_CB(skb)->flow->key.ip.proto;
101 int transport_len = skb->len - skb_transport_offset(skb);
102 if (nw_proto == IPPROTO_TCP) {
103 if (likely(transport_len >= sizeof(struct tcphdr)))
104 return &tcp_hdr(skb)->check;
105 } else if (nw_proto == IPPROTO_UDP) {
106 if (likely(transport_len >= sizeof(struct udphdr)))
107 return &udp_hdr(skb)->check;
112 static int set_nw_addr(struct sk_buff *skb, const struct nlattr *a)
114 __be32 new_nwaddr = nla_get_be32(a);
120 if (unlikely(!is_ip(skb)))
123 err = make_writable(skb, skb_network_offset(skb) +
124 sizeof(struct iphdr));
129 nwaddr = nla_type(a) == ODP_ACTION_ATTR_SET_NW_SRC ? &nh->saddr : &nh->daddr;
131 check = get_l4_checksum(skb);
133 inet_proto_csum_replace4(check, skb, *nwaddr, new_nwaddr, 1);
134 csum_replace4(&nh->check, *nwaddr, new_nwaddr);
136 skb_clear_rxhash(skb);
138 *nwaddr = new_nwaddr;
143 static int set_nw_tos(struct sk_buff *skb, u8 nw_tos)
145 struct iphdr *nh = ip_hdr(skb);
149 if (unlikely(!is_ip(skb)))
152 err = make_writable(skb, skb_network_offset(skb) +
153 sizeof(struct iphdr));
157 /* Set the DSCP bits and preserve the ECN bits. */
159 new = nw_tos | (nh->tos & INET_ECN_MASK);
160 csum_replace4(&nh->check, (__force __be32)old,
161 (__force __be32)new);
167 static int set_tp_port(struct sk_buff *skb, const struct nlattr *a)
174 if (unlikely(!is_ip(skb)))
177 err = make_writable(skb, skb_transport_offset(skb) +
178 sizeof(struct tcphdr));
182 /* Must follow make_writable() since that can move the skb data. */
183 check = get_l4_checksum(skb);
184 if (unlikely(!check))
188 * Update port and checksum.
190 * This is OK because source and destination port numbers are at the
191 * same offsets in both UDP and TCP headers, and get_l4_checksum() only
192 * supports those protocols.
195 port = nla_type(a) == ODP_ACTION_ATTR_SET_TP_SRC ? &th->source : &th->dest;
196 inet_proto_csum_replace2(check, skb, *port, nla_get_be16(a), 0);
197 *port = nla_get_be16(a);
198 skb_clear_rxhash(skb);
203 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
210 p = rcu_dereference(dp->ports[out_port]);
221 static int output_userspace(struct datapath *dp, struct sk_buff *skb, u64 arg)
223 struct dp_upcall_info upcall;
225 skb = skb_clone(skb, GFP_ATOMIC);
229 upcall.cmd = ODP_PACKET_CMD_ACTION;
230 upcall.key = &OVS_CB(skb)->flow->key;
231 upcall.userdata = arg;
232 upcall.sample_pool = 0;
233 upcall.actions = NULL;
234 upcall.actions_len = 0;
235 return dp_upcall(dp, skb, &upcall);
238 /* Execute a list of actions against 'skb'. */
239 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
240 struct sw_flow_actions *acts)
242 /* Every output action needs a separate clone of 'skb', but the common
243 * case is just a single output action, so that doing a clone and
244 * then freeing the original skbuff is wasteful. So the following code
245 * is slightly obscure just to avoid that. */
247 u32 priority = skb->priority;
248 const struct nlattr *a;
251 for (a = acts->actions, rem = acts->actions_len; rem > 0;
252 a = nla_next(a, &rem)) {
255 if (prev_port != -1) {
256 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
260 switch (nla_type(a)) {
261 case ODP_ACTION_ATTR_OUTPUT:
262 prev_port = nla_get_u32(a);
265 case ODP_ACTION_ATTR_USERSPACE:
266 err = output_userspace(dp, skb, nla_get_u64(a));
269 case ODP_ACTION_ATTR_SET_TUNNEL:
270 OVS_CB(skb)->tun_id = nla_get_be64(a);
273 case ODP_ACTION_ATTR_SET_DL_TCI:
274 err = modify_vlan_tci(skb, nla_get_be16(a));
277 case ODP_ACTION_ATTR_STRIP_VLAN:
278 err = strip_vlan(skb);
281 case ODP_ACTION_ATTR_SET_DL_SRC:
282 err = make_writable(skb, ETH_HLEN);
284 memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
287 case ODP_ACTION_ATTR_SET_DL_DST:
288 err = make_writable(skb, ETH_HLEN);
290 memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
293 case ODP_ACTION_ATTR_SET_NW_SRC:
294 case ODP_ACTION_ATTR_SET_NW_DST:
295 err = set_nw_addr(skb, a);
298 case ODP_ACTION_ATTR_SET_NW_TOS:
299 err = set_nw_tos(skb, nla_get_u8(a));
302 case ODP_ACTION_ATTR_SET_TP_SRC:
303 case ODP_ACTION_ATTR_SET_TP_DST:
304 err = set_tp_port(skb, a);
307 case ODP_ACTION_ATTR_SET_PRIORITY:
308 skb->priority = nla_get_u32(a);
311 case ODP_ACTION_ATTR_POP_PRIORITY:
312 skb->priority = priority;
323 do_output(dp, skb, prev_port);
330 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
331 struct sw_flow_actions *acts)
333 struct sk_buff *nskb;
334 struct vport *p = OVS_CB(skb)->vport;
335 struct dp_upcall_info upcall;
340 atomic_inc(&p->sflow_pool);
341 if (net_random() >= dp->sflow_probability)
344 nskb = skb_clone(skb, GFP_ATOMIC);
348 upcall.cmd = ODP_PACKET_CMD_SAMPLE;
349 upcall.key = &OVS_CB(skb)->flow->key;
351 upcall.sample_pool = atomic_read(&p->sflow_pool);
352 upcall.actions = acts->actions;
353 upcall.actions_len = acts->actions_len;
354 dp_upcall(dp, nskb, &upcall);
357 /* Execute a list of actions against 'skb'. */
358 int execute_actions(struct datapath *dp, struct sk_buff *skb)
360 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
361 struct loop_counter *loop;
364 /* Check whether we've looped too much. */
365 loop = loop_get_counter();
366 if (unlikely(++loop->count > MAX_LOOPS))
367 loop->looping = true;
368 if (unlikely(loop->looping)) {
369 error = loop_suppress(dp, acts);
374 /* Really execute actions. */
375 if (dp->sflow_probability)
376 sflow_sample(dp, skb, acts);
377 OVS_CB(skb)->tun_id = 0;
378 error = do_execute_actions(dp, skb, acts);
380 /* Check whether sub-actions looped too much. */
381 if (unlikely(loop->looping))
382 error = loop_suppress(dp, acts);
385 /* Decrement loop counter. */
387 loop->looping = false;