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, bool keep_skb);
42 static int make_writable(struct sk_buff *skb, int write_len)
44 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
47 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
50 /* remove VLAN header from packet and update csum accrodingly. */
51 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
53 struct vlan_hdr *vhdr;
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 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
65 *current_tci = vhdr->h_vlan_TCI;
67 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
68 __skb_pull(skb, VLAN_HLEN);
70 vlan_set_encap_proto(skb, vhdr);
71 skb->mac_header += VLAN_HLEN;
72 skb_reset_mac_len(skb);
77 static int pop_vlan(struct sk_buff *skb)
82 if (likely(vlan_tx_tag_present(skb))) {
85 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
86 skb->len < VLAN_ETH_HLEN))
89 err = __pop_vlan_tci(skb, &tci);
93 /* move next vlan tag to hw accel tag */
94 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
95 skb->len < VLAN_ETH_HLEN))
98 err = __pop_vlan_tci(skb, &tci);
102 __vlan_hwaccel_put_tag(skb, ntohs(tci));
106 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
108 if (unlikely(vlan_tx_tag_present(skb))) {
111 /* push down current VLAN tag */
112 current_tag = vlan_tx_tag_get(skb);
114 if (!__vlan_put_tag(skb, current_tag))
117 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
118 skb->csum = csum_add(skb->csum, csum_partial(skb->data
119 + ETH_HLEN, VLAN_HLEN, 0));
122 __vlan_hwaccel_put_tag(skb, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
126 static int set_eth_addr(struct sk_buff *skb,
127 const struct ovs_key_ethernet *eth_key)
130 err = make_writable(skb, ETH_HLEN);
134 memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN);
135 memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN);
140 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
141 __be32 *addr, __be32 new_addr)
143 int transport_len = skb->len - skb_transport_offset(skb);
145 if (nh->protocol == IPPROTO_TCP) {
146 if (likely(transport_len >= sizeof(struct tcphdr)))
147 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
149 } else if (nh->protocol == IPPROTO_UDP) {
150 if (likely(transport_len >= sizeof(struct udphdr))) {
151 struct udphdr *uh = udp_hdr(skb);
154 get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
155 inet_proto_csum_replace4(&uh->check, skb,
158 uh->check = CSUM_MANGLED_0;
163 csum_replace4(&nh->check, *addr, new_addr);
164 skb_clear_rxhash(skb);
168 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
170 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
174 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
179 err = make_writable(skb, skb_network_offset(skb) +
180 sizeof(struct iphdr));
186 if (ipv4_key->ipv4_src != nh->saddr)
187 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
189 if (ipv4_key->ipv4_dst != nh->daddr)
190 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
192 if (ipv4_key->ipv4_tos != nh->tos)
193 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
195 if (ipv4_key->ipv4_ttl != nh->ttl)
196 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
201 /* Must follow make_writable() since that can move the skb data. */
202 static void set_tp_port(struct sk_buff *skb, __be16 *port,
203 __be16 new_port, __sum16 *check)
205 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
207 skb_clear_rxhash(skb);
210 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
212 struct udphdr *uh = udp_hdr(skb);
214 if (uh->check && get_ip_summed(skb) != OVS_CSUM_PARTIAL) {
215 set_tp_port(skb, port, new_port, &uh->check);
218 uh->check = CSUM_MANGLED_0;
221 skb_clear_rxhash(skb);
225 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
230 err = make_writable(skb, skb_transport_offset(skb) +
231 sizeof(struct udphdr));
236 if (udp_port_key->udp_src != uh->source)
237 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
239 if (udp_port_key->udp_dst != uh->dest)
240 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
245 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
250 err = make_writable(skb, skb_transport_offset(skb) +
251 sizeof(struct tcphdr));
256 if (tcp_port_key->tcp_src != th->source)
257 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
259 if (tcp_port_key->tcp_dst != th->dest)
260 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
265 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
272 vport = ovs_vport_rcu(dp, out_port);
273 if (unlikely(!vport)) {
278 ovs_vport_send(vport, skb);
282 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
283 const struct nlattr *attr)
285 struct dp_upcall_info upcall;
286 const struct nlattr *a;
289 upcall.cmd = OVS_PACKET_CMD_ACTION;
290 upcall.key = &OVS_CB(skb)->flow->key;
291 upcall.userdata = NULL;
294 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
295 a = nla_next(a, &rem)) {
296 switch (nla_type(a)) {
297 case OVS_USERSPACE_ATTR_USERDATA:
301 case OVS_USERSPACE_ATTR_PID:
302 upcall.pid = nla_get_u32(a);
307 return ovs_dp_upcall(dp, skb, &upcall);
310 static int sample(struct datapath *dp, struct sk_buff *skb,
311 const struct nlattr *attr)
313 const struct nlattr *acts_list = NULL;
314 const struct nlattr *a;
317 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
318 a = nla_next(a, &rem)) {
319 switch (nla_type(a)) {
320 case OVS_SAMPLE_ATTR_PROBABILITY:
321 if (net_random() >= nla_get_u32(a))
325 case OVS_SAMPLE_ATTR_ACTIONS:
331 return do_execute_actions(dp, skb, nla_data(acts_list),
332 nla_len(acts_list), true);
335 static int execute_set_action(struct sk_buff *skb,
336 const struct nlattr *nested_attr)
340 switch (nla_type(nested_attr)) {
341 case OVS_KEY_ATTR_PRIORITY:
342 skb->priority = nla_get_u32(nested_attr);
345 case OVS_KEY_ATTR_TUN_ID:
346 OVS_CB(skb)->tun_id = nla_get_be64(nested_attr);
349 case OVS_KEY_ATTR_ETHERNET:
350 err = set_eth_addr(skb, nla_data(nested_attr));
353 case OVS_KEY_ATTR_IPV4:
354 err = set_ipv4(skb, nla_data(nested_attr));
357 case OVS_KEY_ATTR_TCP:
358 err = set_tcp(skb, nla_data(nested_attr));
361 case OVS_KEY_ATTR_UDP:
362 err = set_udp(skb, nla_data(nested_attr));
369 /* Execute a list of actions against 'skb'. */
370 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
371 const struct nlattr *attr, int len, bool keep_skb)
373 /* Every output action needs a separate clone of 'skb', but the common
374 * case is just a single output action, so that doing a clone and
375 * then freeing the original skbuff is wasteful. So the following code
376 * is slightly obscure just to avoid that. */
378 const struct nlattr *a;
381 for (a = attr, rem = len; rem > 0;
382 a = nla_next(a, &rem)) {
385 if (prev_port != -1) {
386 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
390 switch (nla_type(a)) {
391 case OVS_ACTION_ATTR_OUTPUT:
392 prev_port = nla_get_u32(a);
395 case OVS_ACTION_ATTR_USERSPACE:
396 output_userspace(dp, skb, a);
399 case OVS_ACTION_ATTR_PUSH_VLAN:
400 err = push_vlan(skb, nla_data(a));
401 if (unlikely(err)) /* skb already freed. */
405 case OVS_ACTION_ATTR_POP_VLAN:
409 case OVS_ACTION_ATTR_SET:
410 err = execute_set_action(skb, nla_data(a));
413 case OVS_ACTION_ATTR_SAMPLE:
414 err = sample(dp, skb, a);
424 if (prev_port != -1) {
426 skb = skb_clone(skb, GFP_ATOMIC);
428 do_output(dp, skb, prev_port);
429 } else if (!keep_skb)
435 /* We limit the number of times that we pass into execute_actions()
436 * to avoid blowing out the stack in the event that we have a loop. */
439 struct loop_counter {
440 u8 count; /* Count. */
441 bool looping; /* Loop detected? */
444 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
446 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
449 pr_warn("%s: flow looped %d times, dropping\n",
450 ovs_dp_name(dp), MAX_LOOPS);
451 actions->actions_len = 0;
455 /* Execute a list of actions against 'skb'. */
456 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb)
458 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
459 struct loop_counter *loop;
462 /* Check whether we've looped too much. */
463 loop = &__get_cpu_var(loop_counters);
464 if (unlikely(++loop->count > MAX_LOOPS))
465 loop->looping = true;
466 if (unlikely(loop->looping)) {
467 error = loop_suppress(dp, acts);
472 OVS_CB(skb)->tun_id = 0;
473 error = do_execute_actions(dp, skb, acts->actions,
474 acts->actions_len, false);
476 /* Check whether sub-actions looped too much. */
477 if (unlikely(loop->looping))
478 error = loop_suppress(dp, acts);
481 /* Decrement loop counter. */
483 loop->looping = false;