2 * Copyright (c) 2007-2012 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/skbuff.h>
24 #include <linux/openvswitch.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/in6.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_vlan.h>
31 #include <net/checksum.h>
32 #include <net/dsfield.h>
39 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
40 const struct nlattr *attr, int len,
41 struct ovs_key_ipv4_tunnel *tun_key, bool keep_skb);
43 static int make_writable(struct sk_buff *skb, int write_len)
45 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
48 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
51 /* remove VLAN header from packet and update csum accordingly. */
52 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
54 struct vlan_hdr *vhdr;
57 err = make_writable(skb, VLAN_ETH_HLEN);
61 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
62 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
63 + ETH_HLEN, VLAN_HLEN, 0));
65 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
66 *current_tci = vhdr->h_vlan_TCI;
68 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
69 __skb_pull(skb, VLAN_HLEN);
71 vlan_set_encap_proto(skb, vhdr);
72 skb->mac_header += VLAN_HLEN;
73 skb_reset_mac_len(skb);
78 static int pop_vlan(struct sk_buff *skb)
83 if (likely(vlan_tx_tag_present(skb))) {
86 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
87 skb->len < VLAN_ETH_HLEN))
90 err = __pop_vlan_tci(skb, &tci);
94 /* move next vlan tag to hw accel tag */
95 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
96 skb->len < VLAN_ETH_HLEN))
99 err = __pop_vlan_tci(skb, &tci);
103 __vlan_hwaccel_put_tag(skb, ntohs(tci));
107 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
109 if (unlikely(vlan_tx_tag_present(skb))) {
112 /* push down current VLAN tag */
113 current_tag = vlan_tx_tag_get(skb);
115 if (!__vlan_put_tag(skb, current_tag))
118 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
119 skb->csum = csum_add(skb->csum, csum_partial(skb->data
120 + ETH_HLEN, VLAN_HLEN, 0));
123 __vlan_hwaccel_put_tag(skb, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
127 static int set_eth_addr(struct sk_buff *skb,
128 const struct ovs_key_ethernet *eth_key)
131 err = make_writable(skb, ETH_HLEN);
135 memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN);
136 memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN);
141 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
142 __be32 *addr, __be32 new_addr)
144 int transport_len = skb->len - skb_transport_offset(skb);
146 if (nh->protocol == IPPROTO_TCP) {
147 if (likely(transport_len >= sizeof(struct tcphdr)))
148 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
150 } else if (nh->protocol == IPPROTO_UDP) {
151 if (likely(transport_len >= sizeof(struct udphdr))) {
152 struct udphdr *uh = udp_hdr(skb);
155 get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
156 inet_proto_csum_replace4(&uh->check, skb,
159 uh->check = CSUM_MANGLED_0;
164 csum_replace4(&nh->check, *addr, new_addr);
165 skb_clear_rxhash(skb);
169 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
171 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
175 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
180 err = make_writable(skb, skb_network_offset(skb) +
181 sizeof(struct iphdr));
187 if (ipv4_key->ipv4_src != nh->saddr)
188 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
190 if (ipv4_key->ipv4_dst != nh->daddr)
191 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
193 if (ipv4_key->ipv4_tos != nh->tos)
194 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
196 if (ipv4_key->ipv4_ttl != nh->ttl)
197 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
202 /* Must follow make_writable() since that can move the skb data. */
203 static void set_tp_port(struct sk_buff *skb, __be16 *port,
204 __be16 new_port, __sum16 *check)
206 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
208 skb_clear_rxhash(skb);
211 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
213 struct udphdr *uh = udp_hdr(skb);
215 if (uh->check && get_ip_summed(skb) != OVS_CSUM_PARTIAL) {
216 set_tp_port(skb, port, new_port, &uh->check);
219 uh->check = CSUM_MANGLED_0;
222 skb_clear_rxhash(skb);
226 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
231 err = make_writable(skb, skb_transport_offset(skb) +
232 sizeof(struct udphdr));
237 if (udp_port_key->udp_src != uh->source)
238 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
240 if (udp_port_key->udp_dst != uh->dest)
241 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
246 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
251 err = make_writable(skb, skb_transport_offset(skb) +
252 sizeof(struct tcphdr));
257 if (tcp_port_key->tcp_src != th->source)
258 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
260 if (tcp_port_key->tcp_dst != th->dest)
261 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
266 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
273 vport = ovs_vport_rcu(dp, out_port);
274 if (unlikely(!vport)) {
279 ovs_vport_send(vport, skb);
283 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
284 const struct nlattr *attr)
286 struct dp_upcall_info upcall;
287 const struct nlattr *a;
290 upcall.cmd = OVS_PACKET_CMD_ACTION;
291 upcall.key = &OVS_CB(skb)->flow->key;
292 upcall.userdata = NULL;
295 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
296 a = nla_next(a, &rem)) {
297 switch (nla_type(a)) {
298 case OVS_USERSPACE_ATTR_USERDATA:
302 case OVS_USERSPACE_ATTR_PID:
303 upcall.pid = nla_get_u32(a);
308 return ovs_dp_upcall(dp, skb, &upcall);
311 static int sample(struct datapath *dp, struct sk_buff *skb,
312 const struct nlattr *attr,
313 struct ovs_key_ipv4_tunnel *tun_key)
315 const struct nlattr *acts_list = NULL;
316 const struct nlattr *a;
319 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
320 a = nla_next(a, &rem)) {
321 switch (nla_type(a)) {
322 case OVS_SAMPLE_ATTR_PROBABILITY:
323 if (net_random() >= nla_get_u32(a))
327 case OVS_SAMPLE_ATTR_ACTIONS:
333 return do_execute_actions(dp, skb, nla_data(acts_list),
334 nla_len(acts_list), tun_key, true);
337 static int execute_set_action(struct sk_buff *skb,
338 const struct nlattr *nested_attr,
339 struct ovs_key_ipv4_tunnel *tun_key)
343 switch (nla_type(nested_attr)) {
344 case OVS_KEY_ATTR_PRIORITY:
345 skb->priority = nla_get_u32(nested_attr);
348 case OVS_KEY_ATTR_TUN_ID:
349 /* If we're only using the TUN_ID action, store the value in a
350 * temporary instance of struct ovs_key_ipv4_tunnel on the stack.
351 * If both IPV4_TUNNEL and TUN_ID are being used together we
352 * can't write into the IPV4_TUNNEL action, so make a copy and
353 * write into that version.
355 if (!OVS_CB(skb)->tun_key)
356 memset(tun_key, 0, sizeof(*tun_key));
357 else if (OVS_CB(skb)->tun_key != tun_key)
358 memcpy(tun_key, OVS_CB(skb)->tun_key, sizeof(*tun_key));
359 OVS_CB(skb)->tun_key = tun_key;
361 OVS_CB(skb)->tun_key->tun_id = nla_get_be64(nested_attr);
364 case OVS_KEY_ATTR_IPV4_TUNNEL:
365 OVS_CB(skb)->tun_key = nla_data(nested_attr);
368 case OVS_KEY_ATTR_ETHERNET:
369 err = set_eth_addr(skb, nla_data(nested_attr));
372 case OVS_KEY_ATTR_IPV4:
373 err = set_ipv4(skb, nla_data(nested_attr));
376 case OVS_KEY_ATTR_TCP:
377 err = set_tcp(skb, nla_data(nested_attr));
380 case OVS_KEY_ATTR_UDP:
381 err = set_udp(skb, nla_data(nested_attr));
388 /* Execute a list of actions against 'skb'. */
389 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
390 const struct nlattr *attr, int len,
391 struct ovs_key_ipv4_tunnel *tun_key, bool keep_skb)
393 /* Every output action needs a separate clone of 'skb', but the common
394 * case is just a single output action, so that doing a clone and
395 * then freeing the original skbuff is wasteful. So the following code
396 * is slightly obscure just to avoid that. */
398 const struct nlattr *a;
401 for (a = attr, rem = len; rem > 0;
402 a = nla_next(a, &rem)) {
405 if (prev_port != -1) {
406 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
410 switch (nla_type(a)) {
411 case OVS_ACTION_ATTR_OUTPUT:
412 prev_port = nla_get_u32(a);
415 case OVS_ACTION_ATTR_USERSPACE:
416 output_userspace(dp, skb, a);
419 case OVS_ACTION_ATTR_PUSH_VLAN:
420 err = push_vlan(skb, nla_data(a));
421 if (unlikely(err)) /* skb already freed. */
425 case OVS_ACTION_ATTR_POP_VLAN:
429 case OVS_ACTION_ATTR_SET:
430 err = execute_set_action(skb, nla_data(a), tun_key);
433 case OVS_ACTION_ATTR_SAMPLE:
434 err = sample(dp, skb, a, tun_key);
444 if (prev_port != -1) {
446 skb = skb_clone(skb, GFP_ATOMIC);
448 do_output(dp, skb, prev_port);
449 } else if (!keep_skb)
455 /* We limit the number of times that we pass into execute_actions()
456 * to avoid blowing out the stack in the event that we have a loop. */
459 struct loop_counter {
460 u8 count; /* Count. */
461 bool looping; /* Loop detected? */
464 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
466 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
469 pr_warn("%s: flow looped %d times, dropping\n",
470 ovs_dp_name(dp), MAX_LOOPS);
471 actions->actions_len = 0;
475 /* Execute a list of actions against 'skb'. */
476 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb)
478 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
479 struct loop_counter *loop;
481 struct ovs_key_ipv4_tunnel tun_key;
483 /* Check whether we've looped too much. */
484 loop = &__get_cpu_var(loop_counters);
485 if (unlikely(++loop->count > MAX_LOOPS))
486 loop->looping = true;
487 if (unlikely(loop->looping)) {
488 error = loop_suppress(dp, acts);
493 OVS_CB(skb)->tun_key = NULL;
494 error = do_execute_actions(dp, skb, acts->actions,
495 acts->actions_len, &tun_key, false);
497 /* Check whether sub-actions looped too much. */
498 if (unlikely(loop->looping))
499 error = loop_suppress(dp, acts);
502 /* Decrement loop counter. */
504 loop->looping = false;