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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/skbuff.h>
16 #include <linux/openvswitch.h>
17 #include <linux/tcp.h>
18 #include <linux/udp.h>
19 #include <linux/in6.h>
20 #include <linux/if_arp.h>
21 #include <linux/if_vlan.h>
23 #include <net/checksum.h>
24 #include <net/dsfield.h>
32 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
33 const struct nlattr *attr, int len, bool keep_skb);
35 static int make_writable(struct sk_buff *skb, int write_len)
37 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
40 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
43 /* remove VLAN header from packet and update csum accrodingly. */
44 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
47 struct vlan_ethhdr *veth;
50 err = make_writable(skb, VLAN_ETH_HLEN);
54 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
55 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
56 + ETH_HLEN, VLAN_HLEN, 0));
58 veth = (struct vlan_ethhdr *) skb->data;
59 *current_tci = veth->h_vlan_TCI;
61 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
63 eh = (struct ethhdr *)__skb_pull(skb, VLAN_HLEN);
65 skb->protocol = eh->h_proto;
66 skb->mac_header += VLAN_HLEN;
71 static int pop_vlan(struct sk_buff *skb)
76 if (likely(vlan_tx_tag_present(skb))) {
79 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
80 skb->len < VLAN_ETH_HLEN))
83 err = __pop_vlan_tci(skb, &tci);
87 /* move next vlan tag to hw accel tag */
88 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
89 skb->len < VLAN_ETH_HLEN))
92 err = __pop_vlan_tci(skb, &tci);
96 __vlan_hwaccel_put_tag(skb, ntohs(tci));
100 static int push_vlan(struct sk_buff *skb, const struct ovs_key_8021q *q_key)
102 if (unlikely(vlan_tx_tag_present(skb))) {
105 /* push down current VLAN tag */
106 current_tag = vlan_tx_tag_get(skb);
108 if (!__vlan_put_tag(skb, current_tag))
111 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
112 skb->csum = csum_add(skb->csum, csum_partial(skb->data
113 + ETH_HLEN, VLAN_HLEN, 0));
116 __vlan_hwaccel_put_tag(skb, ntohs(q_key->q_tci));
120 static int set_eth_addr(struct sk_buff *skb,
121 const struct ovs_key_ethernet *eth_key)
124 err = make_writable(skb, ETH_HLEN);
128 memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_HLEN);
129 memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_HLEN);
134 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
135 __be32 *addr, __be32 new_addr)
137 int transport_len = skb->len - skb_transport_offset(skb);
139 if (nh->protocol == IPPROTO_TCP) {
140 if (likely(transport_len >= sizeof(struct tcphdr)))
141 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
143 } else if (nh->protocol == IPPROTO_UDP) {
144 if (likely(transport_len >= sizeof(struct udphdr)))
145 inet_proto_csum_replace4(&udp_hdr(skb)->check, skb,
149 csum_replace4(&nh->check, *addr, new_addr);
150 skb_clear_rxhash(skb);
154 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
156 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
160 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
165 err = make_writable(skb, skb_network_offset(skb) +
166 sizeof(struct iphdr));
172 if (ipv4_key->ipv4_src != nh->saddr)
173 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
175 if (ipv4_key->ipv4_dst != nh->daddr)
176 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
178 if (ipv4_key->ipv4_tos != nh->tos)
179 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
181 if (ipv4_key->ipv4_ttl != nh->ttl)
182 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
187 /* Must follow make_writable() since that can move the skb data. */
188 static void set_tp_port(struct sk_buff *skb, __be16 *port,
189 __be16 new_port, __sum16 *check)
191 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
193 skb_clear_rxhash(skb);
196 static int set_udp_port(struct sk_buff *skb,
197 const struct ovs_key_udp *udp_port_key)
202 err = make_writable(skb, skb_transport_offset(skb) +
203 sizeof(struct udphdr));
208 if (udp_port_key->udp_src != uh->source)
209 set_tp_port(skb, &uh->source, udp_port_key->udp_src, &uh->check);
211 if (udp_port_key->udp_dst != uh->dest)
212 set_tp_port(skb, &uh->dest, udp_port_key->udp_dst, &uh->check);
217 static int set_tcp_port(struct sk_buff *skb,
218 const struct ovs_key_tcp *tcp_port_key)
223 err = make_writable(skb, skb_transport_offset(skb) +
224 sizeof(struct tcphdr));
229 if (tcp_port_key->tcp_src != th->source)
230 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
232 if (tcp_port_key->tcp_dst != th->dest)
233 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
238 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
245 vport = rcu_dereference(dp->ports[out_port]);
246 if (unlikely(!vport)) {
251 vport_send(vport, skb);
255 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
256 const struct nlattr *attr)
258 struct dp_upcall_info upcall;
259 const struct nlattr *a;
262 upcall.cmd = OVS_PACKET_CMD_ACTION;
263 upcall.key = &OVS_CB(skb)->flow->key;
264 upcall.userdata = NULL;
267 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
268 a = nla_next(a, &rem)) {
269 switch (nla_type(a)) {
270 case OVS_USERSPACE_ATTR_USERDATA:
274 case OVS_USERSPACE_ATTR_PID:
275 upcall.pid = nla_get_u32(a);
280 return dp_upcall(dp, skb, &upcall);
283 static int sample(struct datapath *dp, struct sk_buff *skb,
284 const struct nlattr *attr)
286 const struct nlattr *acts_list = NULL;
287 const struct nlattr *a;
290 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
291 a = nla_next(a, &rem)) {
292 switch (nla_type(a)) {
293 case OVS_SAMPLE_ATTR_PROBABILITY:
294 if (net_random() >= nla_get_u32(a))
298 case OVS_SAMPLE_ATTR_ACTIONS:
304 return do_execute_actions(dp, skb, nla_data(acts_list),
305 nla_len(acts_list), true);
308 static int execute_set_action(struct sk_buff *skb,
309 const struct nlattr *nested_attr)
313 switch (nla_type(nested_attr)) {
314 case OVS_KEY_ATTR_PRIORITY:
315 skb->priority = nla_get_u32(nested_attr);
318 case OVS_KEY_ATTR_TUN_ID:
319 OVS_CB(skb)->tun_id = nla_get_be64(nested_attr);
322 case OVS_KEY_ATTR_ETHERNET:
323 err = set_eth_addr(skb, nla_data(nested_attr));
326 case OVS_KEY_ATTR_IPV4:
327 err = set_ipv4(skb, nla_data(nested_attr));
330 case OVS_KEY_ATTR_TCP:
331 err = set_tcp_port(skb, nla_data(nested_attr));
334 case OVS_KEY_ATTR_UDP:
335 err = set_udp_port(skb, nla_data(nested_attr));
342 /* Execute a list of actions against 'skb'. */
343 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
344 const struct nlattr *attr, int len, bool keep_skb)
346 /* Every output action needs a separate clone of 'skb', but the common
347 * case is just a single output action, so that doing a clone and
348 * then freeing the original skbuff is wasteful. So the following code
349 * is slightly obscure just to avoid that. */
351 const struct nlattr *a;
354 for (a = attr, rem = len; rem > 0;
355 a = nla_next(a, &rem)) {
358 if (prev_port != -1) {
359 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
363 switch (nla_type(a)) {
364 case OVS_ACTION_ATTR_OUTPUT:
365 prev_port = nla_get_u32(a);
368 case OVS_ACTION_ATTR_USERSPACE:
369 output_userspace(dp, skb, a);
372 case OVS_ACTION_ATTR_PUSH:
373 /* Only supported push action is on vlan tag. */
374 err = push_vlan(skb, nla_data(nla_data(a)));
375 if (unlikely(err)) /* skb already freed. */
379 case OVS_ACTION_ATTR_POP:
380 /* Only supported pop action is on vlan tag. */
384 case OVS_ACTION_ATTR_SET:
385 err = execute_set_action(skb, nla_data(a));
388 case OVS_ACTION_ATTR_SAMPLE:
389 err = sample(dp, skb, a);
399 if (prev_port != -1) {
401 skb = skb_clone(skb, GFP_ATOMIC);
403 do_output(dp, skb, prev_port);
404 } else if (!keep_skb)
410 /* We limit the number of times that we pass into execute_actions()
411 * to avoid blowing out the stack in the event that we have a loop. */
414 struct loop_counter {
415 u8 count; /* Count. */
416 bool looping; /* Loop detected? */
419 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
421 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
424 pr_warn("%s: flow looped %d times, dropping\n",
425 dp_name(dp), MAX_LOOPS);
426 actions->actions_len = 0;
430 /* Execute a list of actions against 'skb'. */
431 int execute_actions(struct datapath *dp, struct sk_buff *skb)
433 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
434 struct loop_counter *loop;
437 /* Check whether we've looped too much. */
438 loop = &__get_cpu_var(loop_counters);
439 if (unlikely(++loop->count > MAX_LOOPS))
440 loop->looping = true;
441 if (unlikely(loop->looping)) {
442 error = loop_suppress(dp, acts);
447 OVS_CB(skb)->tun_id = 0;
448 error = do_execute_actions(dp, skb, acts->actions,
449 acts->actions_len, false);
451 /* Check whether sub-actions looped too much. */
452 if (unlikely(loop->looping))
453 error = loop_suppress(dp, acts);
456 /* Decrement loop counter. */
458 loop->looping = false;