]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/actions.c
dpif-linux: Avoid unaligned accesses to vport stats sent by the datapath.
[mirror_ovs.git] / datapath / actions.c
CommitLineData
064af421
BP
1/*
2 * Distributed under the terms of the GNU GPL version 2.
982b8810 3 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
a14bc59f
BP
4 *
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
064af421
BP
7 */
8
9/* Functions for executing flow actions. */
10
e9141eec
PS
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
064af421
BP
13#include <linux/skbuff.h>
14#include <linux/in.h>
15#include <linux/ip.h>
16#include <linux/tcp.h>
17#include <linux/udp.h>
18#include <linux/in6.h>
401eeb92 19#include <linux/if_arp.h>
064af421 20#include <linux/if_vlan.h>
f1193301 21#include <net/inet_ecn.h>
064af421
BP
22#include <net/ip.h>
23#include <net/checksum.h>
f2459fe7 24
064af421 25#include "actions.h"
dd8d6b8c 26#include "checksum.h"
f2459fe7 27#include "datapath.h"
064af421 28#include "openvswitch/datapath-protocol.h"
6ce39213 29#include "vlan.h"
f2459fe7 30#include "vport.h"
064af421 31
6ff686f2
PS
32static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
33 const struct nlattr *attr, int len, bool keep_skb);
871dfe07 34
10db8b20 35static int make_writable(struct sk_buff *skb, int write_len)
064af421 36{
10db8b20
JG
37 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
38 return 0;
0cd8a05e 39
10db8b20 40 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
064af421
BP
41}
42
d9065a90
PS
43/* remove VLAN header from packet and update csum accrodingly. */
44static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
064af421 45{
064af421 46 struct ethhdr *eh;
d9065a90 47 struct vlan_ethhdr *veth;
10db8b20 48 int err;
064af421 49
10db8b20
JG
50 err = make_writable(skb, VLAN_ETH_HLEN);
51 if (unlikely(err))
52 return err;
6ce39213 53
dd8d6b8c 54 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
635c9298
JG
55 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
56 + ETH_HLEN, VLAN_HLEN, 0));
57
d9065a90
PS
58 veth = (struct vlan_ethhdr *) skb->data;
59 *current_tci = veth->h_vlan_TCI;
60
6ce39213 61 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
064af421 62
d9065a90 63 eh = (struct ethhdr *)__skb_pull(skb, VLAN_HLEN);
064af421
BP
64
65 skb->protocol = eh->h_proto;
66 skb->mac_header += VLAN_HLEN;
67
10db8b20 68 return 0;
064af421
BP
69}
70
d9065a90 71static int pop_vlan(struct sk_buff *skb)
064af421 72{
d9065a90
PS
73 __be16 tci;
74 int err;
10db8b20 75
d9065a90
PS
76 if (likely(vlan_tx_tag_present(skb))) {
77 vlan_set_tci(skb, 0);
78 } else {
79 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
80 skb->len < VLAN_ETH_HLEN))
10db8b20 81 return 0;
064af421 82
d9065a90
PS
83 err = __pop_vlan_tci(skb, &tci);
84 if (err)
10db8b20 85 return err;
064af421 86 }
d9065a90
PS
87 /* move next vlan tag to hw accel tag */
88 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
89 skb->len < VLAN_ETH_HLEN))
90 return 0;
91
92 err = __pop_vlan_tci(skb, &tci);
93 if (unlikely(err))
94 return err;
064af421 95
10db8b20 96 __vlan_hwaccel_put_tag(skb, ntohs(tci));
d9065a90
PS
97 return 0;
98}
99
100static int push_vlan(struct sk_buff *skb, __be16 new_tci)
101{
102 if (unlikely(vlan_tx_tag_present(skb))) {
103 u16 current_tag;
104
105 /* push down current VLAN tag */
106 current_tag = vlan_tx_tag_get(skb);
107
108 if (!__vlan_put_tag(skb, current_tag))
109 return -ENOMEM;
10db8b20 110
d9065a90
PS
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));
114
115 }
116 __vlan_hwaccel_put_tag(skb, ntohs(new_tci));
10db8b20 117 return 0;
064af421
BP
118}
119
a4af2475 120static bool is_ip(struct sk_buff *skb)
ca78c6b6 121{
76abe283 122 return (OVS_CB(skb)->flow->key.eth.type == htons(ETH_P_IP) &&
ca78c6b6
BP
123 skb->transport_header > skb->network_header);
124}
125
a4af2475 126static __sum16 *get_l4_checksum(struct sk_buff *skb)
ca78c6b6 127{
28bad473 128 u8 nw_proto = OVS_CB(skb)->flow->key.ip.proto;
ca78c6b6 129 int transport_len = skb->len - skb_transport_offset(skb);
a4af2475 130 if (nw_proto == IPPROTO_TCP) {
ca78c6b6
BP
131 if (likely(transport_len >= sizeof(struct tcphdr)))
132 return &tcp_hdr(skb)->check;
a4af2475 133 } else if (nw_proto == IPPROTO_UDP) {
ca78c6b6
BP
134 if (likely(transport_len >= sizeof(struct udphdr)))
135 return &udp_hdr(skb)->check;
136 }
137 return NULL;
138}
139
10db8b20 140static int set_nw_addr(struct sk_buff *skb, const struct nlattr *a)
064af421 141{
cdee00fd 142 __be32 new_nwaddr = nla_get_be32(a);
ca78c6b6
BP
143 struct iphdr *nh;
144 __sum16 *check;
145 __be32 *nwaddr;
10db8b20 146 int err;
ca78c6b6 147
a4af2475 148 if (unlikely(!is_ip(skb)))
10db8b20 149 return 0;
064af421 150
10db8b20
JG
151 err = make_writable(skb, skb_network_offset(skb) +
152 sizeof(struct iphdr));
153 if (unlikely(err))
154 return err;
ca78c6b6
BP
155
156 nh = ip_hdr(skb);
df2c07f4 157 nwaddr = nla_type(a) == OVS_ACTION_ATTR_SET_NW_SRC ? &nh->saddr : &nh->daddr;
ca78c6b6 158
a4af2475 159 check = get_l4_checksum(skb);
ca78c6b6 160 if (likely(check))
cdee00fd
BP
161 inet_proto_csum_replace4(check, skb, *nwaddr, new_nwaddr, 1);
162 csum_replace4(&nh->check, *nwaddr, new_nwaddr);
ca78c6b6 163
a4a26436
SH
164 skb_clear_rxhash(skb);
165
cdee00fd 166 *nwaddr = new_nwaddr;
ca78c6b6 167
10db8b20 168 return 0;
064af421
BP
169}
170
10db8b20 171static int set_nw_tos(struct sk_buff *skb, u8 nw_tos)
959a2ecd 172{
10db8b20
JG
173 struct iphdr *nh = ip_hdr(skb);
174 u8 old, new;
175 int err;
176
a4af2475 177 if (unlikely(!is_ip(skb)))
10db8b20
JG
178 return 0;
179
180 err = make_writable(skb, skb_network_offset(skb) +
181 sizeof(struct iphdr));
182 if (unlikely(err))
183 return err;
184
185 /* Set the DSCP bits and preserve the ECN bits. */
186 old = nh->tos;
187 new = nw_tos | (nh->tos & INET_ECN_MASK);
188 csum_replace4(&nh->check, (__force __be32)old,
189 (__force __be32)new);
190 nh->tos = new;
191
192 return 0;
959a2ecd
JP
193}
194
10db8b20 195static int set_tp_port(struct sk_buff *skb, const struct nlattr *a)
064af421 196{
ca78c6b6
BP
197 struct udphdr *th;
198 __sum16 *check;
199 __be16 *port;
10db8b20 200 int err;
064af421 201
a4af2475 202 if (unlikely(!is_ip(skb)))
10db8b20 203 return 0;
064af421 204
10db8b20
JG
205 err = make_writable(skb, skb_transport_offset(skb) +
206 sizeof(struct tcphdr));
207 if (unlikely(err))
208 return err;
ca78c6b6
BP
209
210 /* Must follow make_writable() since that can move the skb data. */
a4af2475 211 check = get_l4_checksum(skb);
ca78c6b6 212 if (unlikely(!check))
10db8b20 213 return 0;
064af421 214
ca78c6b6
BP
215 /*
216 * Update port and checksum.
217 *
218 * This is OK because source and destination port numbers are at the
219 * same offsets in both UDP and TCP headers, and get_l4_checksum() only
220 * supports those protocols.
221 */
222 th = udp_hdr(skb);
df2c07f4 223 port = nla_type(a) == OVS_ACTION_ATTR_SET_TP_SRC ? &th->source : &th->dest;
cdee00fd
BP
224 inet_proto_csum_replace2(check, skb, *port, nla_get_be16(a), 0);
225 *port = nla_get_be16(a);
a4a26436 226 skb_clear_rxhash(skb);
ca78c6b6 227
10db8b20 228 return 0;
064af421
BP
229}
230
f15c8639 231static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
064af421 232{
f15c8639 233 struct vport *vport;
064af421 234
f15c8639
JG
235 if (unlikely(!skb))
236 return -ENOMEM;
064af421 237
f15c8639
JG
238 vport = rcu_dereference(dp->ports[out_port]);
239 if (unlikely(!vport)) {
240 kfree_skb(skb);
241 return -ENODEV;
242 }
064af421 243
f15c8639
JG
244 vport_send(vport, skb);
245 return 0;
064af421
BP
246}
247
b85d8d61 248static int output_userspace(struct datapath *dp, struct sk_buff *skb, u64 arg)
064af421 249{
856081f6
BP
250 struct dp_upcall_info upcall;
251
df2c07f4 252 upcall.cmd = OVS_PACKET_CMD_ACTION;
a4af2475 253 upcall.key = &OVS_CB(skb)->flow->key;
856081f6 254 upcall.userdata = arg;
856081f6 255 return dp_upcall(dp, skb, &upcall);
064af421
BP
256}
257
6ff686f2
PS
258static int sample(struct datapath *dp, struct sk_buff *skb,
259 const struct nlattr *attr)
260{
261 const struct nlattr *acts_list = NULL;
262 const struct nlattr *a;
263 int rem;
264
265 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
266 a = nla_next(a, &rem)) {
267 switch (nla_type(a)) {
268 case OVS_SAMPLE_ATTR_PROBABILITY:
269 if (net_random() >= nla_get_u32(a))
270 return 0;
271 break;
272
273 case OVS_SAMPLE_ATTR_ACTIONS:
274 acts_list = a;
275 break;
276 }
277 }
278
279 return do_execute_actions(dp, skb, nla_data(acts_list),
280 nla_len(acts_list), true);
281}
282
064af421 283/* Execute a list of actions against 'skb'. */
871dfe07 284static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
6ff686f2 285 const struct nlattr *attr, int len, bool keep_skb)
064af421
BP
286{
287 /* Every output action needs a separate clone of 'skb', but the common
288 * case is just a single output action, so that doing a clone and
289 * then freeing the original skbuff is wasteful. So the following code
290 * is slightly obscure just to avoid that. */
291 int prev_port = -1;
c1c9c9c4 292 u32 priority = skb->priority;
cdee00fd 293 const struct nlattr *a;
10db8b20 294 int rem;
72b06300 295
6ff686f2 296 for (a = attr, rem = len; rem > 0;
a4af2475 297 a = nla_next(a, &rem)) {
10db8b20
JG
298 int err = 0;
299
064af421 300 if (prev_port != -1) {
7956695a 301 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
064af421
BP
302 prev_port = -1;
303 }
304
cdee00fd 305 switch (nla_type(a)) {
df2c07f4 306 case OVS_ACTION_ATTR_OUTPUT:
cdee00fd 307 prev_port = nla_get_u32(a);
064af421
BP
308 break;
309
df2c07f4 310 case OVS_ACTION_ATTR_USERSPACE:
646b8753 311 output_userspace(dp, skb, nla_get_u64(a));
064af421
BP
312 break;
313
df2c07f4 314 case OVS_ACTION_ATTR_SET_TUNNEL:
b9298d3f 315 OVS_CB(skb)->tun_id = nla_get_be64(a);
659586ef
JG
316 break;
317
d9065a90
PS
318 case OVS_ACTION_ATTR_PUSH_VLAN:
319 err = push_vlan(skb, nla_get_be16(a));
320 if (unlikely(err)) /* skb already freed */
321 return err;
064af421
BP
322 break;
323
d9065a90
PS
324 case OVS_ACTION_ATTR_POP_VLAN:
325 err = pop_vlan(skb);
064af421
BP
326 break;
327
df2c07f4 328 case OVS_ACTION_ATTR_SET_DL_SRC:
10db8b20
JG
329 err = make_writable(skb, ETH_HLEN);
330 if (likely(!err))
331 memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
cdee00fd
BP
332 break;
333
df2c07f4 334 case OVS_ACTION_ATTR_SET_DL_DST:
10db8b20
JG
335 err = make_writable(skb, ETH_HLEN);
336 if (likely(!err))
337 memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
064af421
BP
338 break;
339
df2c07f4
JP
340 case OVS_ACTION_ATTR_SET_NW_SRC:
341 case OVS_ACTION_ATTR_SET_NW_DST:
10db8b20 342 err = set_nw_addr(skb, a);
064af421
BP
343 break;
344
df2c07f4 345 case OVS_ACTION_ATTR_SET_NW_TOS:
10db8b20 346 err = set_nw_tos(skb, nla_get_u8(a));
959a2ecd
JP
347 break;
348
df2c07f4
JP
349 case OVS_ACTION_ATTR_SET_TP_SRC:
350 case OVS_ACTION_ATTR_SET_TP_DST:
10db8b20 351 err = set_tp_port(skb, a);
064af421 352 break;
c1c9c9c4 353
df2c07f4 354 case OVS_ACTION_ATTR_SET_PRIORITY:
cdee00fd 355 skb->priority = nla_get_u32(a);
c1c9c9c4
BP
356 break;
357
df2c07f4 358 case OVS_ACTION_ATTR_POP_PRIORITY:
c1c9c9c4
BP
359 skb->priority = priority;
360 break;
10db8b20 361
6ff686f2
PS
362 case OVS_ACTION_ATTR_SAMPLE:
363 err = sample(dp, skb, a);
364 break;
365
366 }
10db8b20
JG
367 if (unlikely(err)) {
368 kfree_skb(skb);
369 return err;
370 }
064af421 371 }
6c222e55 372
6ff686f2
PS
373 if (prev_port != -1) {
374 if (keep_skb)
375 skb = skb_clone(skb, GFP_ATOMIC);
376
064af421 377 do_output(dp, skb, prev_port);
6ff686f2 378 } else if (!keep_skb)
5b95ab0e 379 consume_skb(skb);
10db8b20 380
a5225dd6 381 return 0;
064af421 382}
871dfe07 383
e9141eec
PS
384/* We limit the number of times that we pass into execute_actions()
385 * to avoid blowing out the stack in the event that we have a loop. */
386#define MAX_LOOPS 5
387
388struct loop_counter {
389 u8 count; /* Count. */
390 bool looping; /* Loop detected? */
391};
392
393static DEFINE_PER_CPU(struct loop_counter, loop_counters);
394
395static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
396{
397 if (net_ratelimit())
398 pr_warn("%s: flow looped %d times, dropping\n",
399 dp_name(dp), MAX_LOOPS);
400 actions->actions_len = 0;
401 return -ELOOP;
402}
403
871dfe07 404/* Execute a list of actions against 'skb'. */
a4af2475 405int execute_actions(struct datapath *dp, struct sk_buff *skb)
871dfe07 406{
a4af2475
BP
407 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
408 struct loop_counter *loop;
409 int error;
410
411 /* Check whether we've looped too much. */
e9141eec 412 loop = &__get_cpu_var(loop_counters);
a4af2475
BP
413 if (unlikely(++loop->count > MAX_LOOPS))
414 loop->looping = true;
415 if (unlikely(loop->looping)) {
416 error = loop_suppress(dp, acts);
417 kfree_skb(skb);
418 goto out_loop;
419 }
871dfe07
BP
420
421 OVS_CB(skb)->tun_id = 0;
6ff686f2
PS
422 error = do_execute_actions(dp, skb, acts->actions,
423 acts->actions_len, false);
a4af2475
BP
424
425 /* Check whether sub-actions looped too much. */
426 if (unlikely(loop->looping))
427 error = loop_suppress(dp, acts);
428
429out_loop:
430 /* Decrement loop counter. */
431 if (!--loop->count)
432 loop->looping = false;
871dfe07 433
a4af2475 434 return error;
871dfe07 435}