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 /* remove VLAN header from packet and update csum accrodingly. */
43 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
46 struct vlan_ethhdr *veth;
49 err = make_writable(skb, VLAN_ETH_HLEN);
53 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
54 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
55 + ETH_HLEN, VLAN_HLEN, 0));
57 veth = (struct vlan_ethhdr *) skb->data;
58 *current_tci = veth->h_vlan_TCI;
60 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
62 eh = (struct ethhdr *)__skb_pull(skb, VLAN_HLEN);
64 skb->protocol = eh->h_proto;
65 skb->mac_header += VLAN_HLEN;
70 static int pop_vlan(struct sk_buff *skb)
75 if (likely(vlan_tx_tag_present(skb))) {
78 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
79 skb->len < VLAN_ETH_HLEN))
82 err = __pop_vlan_tci(skb, &tci);
86 /* move next vlan tag to hw accel tag */
87 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
88 skb->len < VLAN_ETH_HLEN))
91 err = __pop_vlan_tci(skb, &tci);
95 __vlan_hwaccel_put_tag(skb, ntohs(tci));
99 static int push_vlan(struct sk_buff *skb, __be16 new_tci)
101 if (unlikely(vlan_tx_tag_present(skb))) {
104 /* push down current VLAN tag */
105 current_tag = vlan_tx_tag_get(skb);
107 if (!__vlan_put_tag(skb, current_tag))
110 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
111 skb->csum = csum_add(skb->csum, csum_partial(skb->data
112 + ETH_HLEN, VLAN_HLEN, 0));
115 __vlan_hwaccel_put_tag(skb, ntohs(new_tci));
119 static bool is_ip(struct sk_buff *skb)
121 return (OVS_CB(skb)->flow->key.eth.type == htons(ETH_P_IP) &&
122 skb->transport_header > skb->network_header);
125 static __sum16 *get_l4_checksum(struct sk_buff *skb)
127 u8 nw_proto = OVS_CB(skb)->flow->key.ip.proto;
128 int transport_len = skb->len - skb_transport_offset(skb);
129 if (nw_proto == IPPROTO_TCP) {
130 if (likely(transport_len >= sizeof(struct tcphdr)))
131 return &tcp_hdr(skb)->check;
132 } else if (nw_proto == IPPROTO_UDP) {
133 if (likely(transport_len >= sizeof(struct udphdr)))
134 return &udp_hdr(skb)->check;
139 static int set_nw_addr(struct sk_buff *skb, const struct nlattr *a)
141 __be32 new_nwaddr = nla_get_be32(a);
147 if (unlikely(!is_ip(skb)))
150 err = make_writable(skb, skb_network_offset(skb) +
151 sizeof(struct iphdr));
156 nwaddr = nla_type(a) == OVS_ACTION_ATTR_SET_NW_SRC ? &nh->saddr : &nh->daddr;
158 check = get_l4_checksum(skb);
160 inet_proto_csum_replace4(check, skb, *nwaddr, new_nwaddr, 1);
161 csum_replace4(&nh->check, *nwaddr, new_nwaddr);
163 skb_clear_rxhash(skb);
165 *nwaddr = new_nwaddr;
170 static int set_nw_tos(struct sk_buff *skb, u8 nw_tos)
172 struct iphdr *nh = ip_hdr(skb);
176 if (unlikely(!is_ip(skb)))
179 err = make_writable(skb, skb_network_offset(skb) +
180 sizeof(struct iphdr));
184 /* Set the DSCP bits and preserve the ECN bits. */
186 new = nw_tos | (nh->tos & INET_ECN_MASK);
187 csum_replace4(&nh->check, (__force __be32)old,
188 (__force __be32)new);
194 static int set_tp_port(struct sk_buff *skb, const struct nlattr *a)
201 if (unlikely(!is_ip(skb)))
204 err = make_writable(skb, skb_transport_offset(skb) +
205 sizeof(struct tcphdr));
209 /* Must follow make_writable() since that can move the skb data. */
210 check = get_l4_checksum(skb);
211 if (unlikely(!check))
215 * Update port and checksum.
217 * This is OK because source and destination port numbers are at the
218 * same offsets in both UDP and TCP headers, and get_l4_checksum() only
219 * supports those protocols.
222 port = nla_type(a) == OVS_ACTION_ATTR_SET_TP_SRC ? &th->source : &th->dest;
223 inet_proto_csum_replace2(check, skb, *port, nla_get_be16(a), 0);
224 *port = nla_get_be16(a);
225 skb_clear_rxhash(skb);
230 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
237 vport = rcu_dereference(dp->ports[out_port]);
238 if (unlikely(!vport)) {
243 vport_send(vport, skb);
247 static int output_userspace(struct datapath *dp, struct sk_buff *skb, u64 arg)
249 struct dp_upcall_info upcall;
251 skb = skb_clone(skb, GFP_ATOMIC);
255 upcall.cmd = OVS_PACKET_CMD_ACTION;
256 upcall.key = &OVS_CB(skb)->flow->key;
257 upcall.userdata = arg;
258 upcall.sample_pool = 0;
259 upcall.actions = NULL;
260 upcall.actions_len = 0;
261 return dp_upcall(dp, skb, &upcall);
264 /* Execute a list of actions against 'skb'. */
265 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
266 struct sw_flow_actions *acts)
268 /* Every output action needs a separate clone of 'skb', but the common
269 * case is just a single output action, so that doing a clone and
270 * then freeing the original skbuff is wasteful. So the following code
271 * is slightly obscure just to avoid that. */
273 u32 priority = skb->priority;
274 const struct nlattr *a;
277 for (a = acts->actions, rem = acts->actions_len; rem > 0;
278 a = nla_next(a, &rem)) {
281 if (prev_port != -1) {
282 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
286 switch (nla_type(a)) {
287 case OVS_ACTION_ATTR_OUTPUT:
288 prev_port = nla_get_u32(a);
291 case OVS_ACTION_ATTR_USERSPACE:
292 output_userspace(dp, skb, nla_get_u64(a));
295 case OVS_ACTION_ATTR_SET_TUNNEL:
296 OVS_CB(skb)->tun_id = nla_get_be64(a);
299 case OVS_ACTION_ATTR_PUSH_VLAN:
300 err = push_vlan(skb, nla_get_be16(a));
301 if (unlikely(err)) /* skb already freed */
305 case OVS_ACTION_ATTR_POP_VLAN:
309 case OVS_ACTION_ATTR_SET_DL_SRC:
310 err = make_writable(skb, ETH_HLEN);
312 memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
315 case OVS_ACTION_ATTR_SET_DL_DST:
316 err = make_writable(skb, ETH_HLEN);
318 memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
321 case OVS_ACTION_ATTR_SET_NW_SRC:
322 case OVS_ACTION_ATTR_SET_NW_DST:
323 err = set_nw_addr(skb, a);
326 case OVS_ACTION_ATTR_SET_NW_TOS:
327 err = set_nw_tos(skb, nla_get_u8(a));
330 case OVS_ACTION_ATTR_SET_TP_SRC:
331 case OVS_ACTION_ATTR_SET_TP_DST:
332 err = set_tp_port(skb, a);
335 case OVS_ACTION_ATTR_SET_PRIORITY:
336 skb->priority = nla_get_u32(a);
339 case OVS_ACTION_ATTR_POP_PRIORITY:
340 skb->priority = priority;
351 do_output(dp, skb, prev_port);
358 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
359 struct sw_flow_actions *acts)
361 struct sk_buff *nskb;
362 struct vport *p = OVS_CB(skb)->vport;
363 struct dp_upcall_info upcall;
368 atomic_inc(&p->sflow_pool);
369 if (net_random() >= dp->sflow_probability)
372 nskb = skb_clone(skb, GFP_ATOMIC);
376 upcall.cmd = OVS_PACKET_CMD_SAMPLE;
377 upcall.key = &OVS_CB(skb)->flow->key;
379 upcall.sample_pool = atomic_read(&p->sflow_pool);
380 upcall.actions = acts->actions;
381 upcall.actions_len = acts->actions_len;
382 dp_upcall(dp, nskb, &upcall);
385 /* Execute a list of actions against 'skb'. */
386 int execute_actions(struct datapath *dp, struct sk_buff *skb)
388 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
389 struct loop_counter *loop;
392 /* Check whether we've looped too much. */
393 loop = loop_get_counter();
394 if (unlikely(++loop->count > MAX_LOOPS))
395 loop->looping = true;
396 if (unlikely(loop->looping)) {
397 error = loop_suppress(dp, acts);
402 /* Really execute actions. */
403 if (dp->sflow_probability)
404 sflow_sample(dp, skb, acts);
405 OVS_CB(skb)->tun_id = 0;
406 error = do_execute_actions(dp, skb, acts);
408 /* Check whether sub-actions looped too much. */
409 if (unlikely(loop->looping))
410 error = loop_suppress(dp, acts);
413 /* Decrement loop counter. */
415 loop->looping = false;