datapath: Convert kernel priority actions into match/set.
[sliver-openvswitch.git] / datapath / actions.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 /* Functions for executing flow actions. */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/skbuff.h>
14 #include <linux/in.h>
15 #include <linux/ip.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>
22 #include <net/inet_ecn.h>
23 #include <net/ip.h>
24 #include <net/checksum.h>
25
26 #include "actions.h"
27 #include "checksum.h"
28 #include "datapath.h"
29 #include "vlan.h"
30 #include "vport.h"
31
32 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
33                         const struct nlattr *attr, int len, bool keep_skb);
34
35 static int make_writable(struct sk_buff *skb, int write_len)
36 {
37         if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
38                 return 0;
39
40         return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
41 }
42
43 /* remove VLAN header from packet and update csum accrodingly. */
44 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
45 {
46         struct ethhdr *eh;
47         struct vlan_ethhdr *veth;
48         int err;
49
50         err = make_writable(skb, VLAN_ETH_HLEN);
51         if (unlikely(err))
52                 return err;
53
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));
57
58         veth = (struct vlan_ethhdr *) skb->data;
59         *current_tci = veth->h_vlan_TCI;
60
61         memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
62
63         eh = (struct ethhdr *)__skb_pull(skb, VLAN_HLEN);
64
65         skb->protocol = eh->h_proto;
66         skb->mac_header += VLAN_HLEN;
67
68         return 0;
69 }
70
71 static int pop_vlan(struct sk_buff *skb)
72 {
73         __be16 tci;
74         int err;
75
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))
81                         return 0;
82
83                 err = __pop_vlan_tci(skb, &tci);
84                 if (err)
85                         return err;
86         }
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;
95
96         __vlan_hwaccel_put_tag(skb, ntohs(tci));
97         return 0;
98 }
99
100 static int push_vlan(struct sk_buff *skb, const struct ovs_key_8021q *q_key)
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;
110
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(q_key->q_tci));
117         return 0;
118 }
119
120 static int set_eth_addr(struct sk_buff *skb,
121                         const struct ovs_key_ethernet *eth_key)
122 {
123         int err;
124         err = make_writable(skb, ETH_HLEN);
125         if (unlikely(err))
126                 return err;
127
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);
130
131         return 0;
132 }
133
134 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
135                                 __be32 *addr, __be32 new_addr)
136 {
137         int transport_len = skb->len - skb_transport_offset(skb);
138
139         if (nh->protocol == IPPROTO_TCP) {
140                 if (likely(transport_len >= sizeof(struct tcphdr)))
141                         inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
142                                                  *addr, new_addr, 1);
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,
146                                                  *addr, new_addr, 1);
147         }
148
149         csum_replace4(&nh->check, *addr, new_addr);
150         skb_clear_rxhash(skb);
151         *addr = new_addr;
152 }
153
154 static void set_ip_tos(struct sk_buff *skb, struct iphdr *nh, u8 new_tos)
155 {
156         u8 old, new;
157
158         /* Set the DSCP bits and preserve the ECN bits. */
159         old = nh->tos;
160         new = new_tos | (nh->tos & INET_ECN_MASK);
161         csum_replace4(&nh->check, (__force __be32)old,
162                                   (__force __be32)new);
163         nh->tos = new;
164 }
165
166 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
167 {
168         struct iphdr *nh;
169         int err;
170
171         err = make_writable(skb, skb_network_offset(skb) +
172                                  sizeof(struct iphdr));
173         if (unlikely(err))
174                 return err;
175
176         nh = ip_hdr(skb);
177
178         if (ipv4_key->ipv4_src != nh->saddr)
179                 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
180
181         if (ipv4_key->ipv4_dst != nh->daddr)
182                 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
183
184         if (ipv4_key->ipv4_tos != nh->tos)
185                 set_ip_tos(skb, nh, ipv4_key->ipv4_tos);
186
187         return 0;
188 }
189
190 /* Must follow make_writable() since that can move the skb data. */
191 static void set_tp_port(struct sk_buff *skb, __be16 *port,
192                          __be16 new_port, __sum16 *check)
193 {
194         inet_proto_csum_replace2(check, skb, *port, new_port, 0);
195         *port = new_port;
196         skb_clear_rxhash(skb);
197 }
198
199 static int set_udp_port(struct sk_buff *skb,
200                         const struct ovs_key_udp *udp_port_key)
201 {
202         struct udphdr *uh;
203         int err;
204
205         err = make_writable(skb, skb_transport_offset(skb) +
206                                  sizeof(struct udphdr));
207         if (unlikely(err))
208                 return err;
209
210         uh = udp_hdr(skb);
211         if (udp_port_key->udp_src != uh->source)
212                 set_tp_port(skb, &uh->source, udp_port_key->udp_src, &uh->check);
213
214         if (udp_port_key->udp_dst != uh->dest)
215                 set_tp_port(skb, &uh->dest, udp_port_key->udp_dst, &uh->check);
216
217         return 0;
218 }
219
220 static int set_tcp_port(struct sk_buff *skb,
221                         const struct ovs_key_tcp *tcp_port_key)
222 {
223         struct tcphdr *th;
224         int err;
225
226         err = make_writable(skb, skb_transport_offset(skb) +
227                                  sizeof(struct tcphdr));
228         if (unlikely(err))
229                 return err;
230
231         th = tcp_hdr(skb);
232         if (tcp_port_key->tcp_src != th->source)
233                 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
234
235         if (tcp_port_key->tcp_dst != th->dest)
236                 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
237
238         return 0;
239 }
240
241 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
242 {
243         struct vport *vport;
244
245         if (unlikely(!skb))
246                 return -ENOMEM;
247
248         vport = rcu_dereference(dp->ports[out_port]);
249         if (unlikely(!vport)) {
250                 kfree_skb(skb);
251                 return -ENODEV;
252         }
253
254         vport_send(vport, skb);
255         return 0;
256 }
257
258 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
259                             const struct nlattr *attr)
260 {
261         struct dp_upcall_info upcall;
262         const struct nlattr *a;
263         int rem;
264
265         upcall.cmd = OVS_PACKET_CMD_ACTION;
266         upcall.key = &OVS_CB(skb)->flow->key;
267         upcall.userdata = NULL;
268         upcall.pid = 0;
269
270         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
271                  a = nla_next(a, &rem)) {
272                 switch (nla_type(a)) {
273                 case OVS_USERSPACE_ATTR_USERDATA:
274                         upcall.userdata = a;
275                         break;
276
277                 case OVS_USERSPACE_ATTR_PID:
278                         upcall.pid = nla_get_u32(a);
279                         break;
280                 }
281         }
282
283         return dp_upcall(dp, skb, &upcall);
284 }
285
286 static int sample(struct datapath *dp, struct sk_buff *skb,
287                   const struct nlattr *attr)
288 {
289         const struct nlattr *acts_list = NULL;
290         const struct nlattr *a;
291         int rem;
292
293         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
294                  a = nla_next(a, &rem)) {
295                 switch (nla_type(a)) {
296                 case OVS_SAMPLE_ATTR_PROBABILITY:
297                         if (net_random() >= nla_get_u32(a))
298                                 return 0;
299                         break;
300
301                 case OVS_SAMPLE_ATTR_ACTIONS:
302                         acts_list = a;
303                         break;
304                 }
305         }
306
307         return do_execute_actions(dp, skb, nla_data(acts_list),
308                                                  nla_len(acts_list), true);
309 }
310
311 static int execute_set_action(struct sk_buff *skb,
312                                  const struct nlattr *nested_attr)
313 {
314         int err = 0;
315
316         switch (nla_type(nested_attr)) {
317         case OVS_KEY_ATTR_PRIORITY:
318                 skb->priority = nla_get_u32(nested_attr);
319                 break;
320
321         case OVS_KEY_ATTR_TUN_ID:
322                 OVS_CB(skb)->tun_id = nla_get_be64(nested_attr);
323                 break;
324
325         case OVS_KEY_ATTR_ETHERNET:
326                 err = set_eth_addr(skb, nla_data(nested_attr));
327                 break;
328
329         case OVS_KEY_ATTR_IPV4:
330                 err = set_ipv4(skb, nla_data(nested_attr));
331                 break;
332
333         case OVS_KEY_ATTR_TCP:
334                 err = set_tcp_port(skb, nla_data(nested_attr));
335                 break;
336
337         case OVS_KEY_ATTR_UDP:
338                 err = set_udp_port(skb, nla_data(nested_attr));
339                 break;
340         }
341
342         return err;
343 }
344
345 /* Execute a list of actions against 'skb'. */
346 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
347                         const struct nlattr *attr, int len, bool keep_skb)
348 {
349         /* Every output action needs a separate clone of 'skb', but the common
350          * case is just a single output action, so that doing a clone and
351          * then freeing the original skbuff is wasteful.  So the following code
352          * is slightly obscure just to avoid that. */
353         int prev_port = -1;
354         const struct nlattr *a;
355         int rem;
356
357         for (a = attr, rem = len; rem > 0;
358              a = nla_next(a, &rem)) {
359                 int err = 0;
360
361                 if (prev_port != -1) {
362                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
363                         prev_port = -1;
364                 }
365
366                 switch (nla_type(a)) {
367                 case OVS_ACTION_ATTR_OUTPUT:
368                         prev_port = nla_get_u32(a);
369                         break;
370
371                 case OVS_ACTION_ATTR_USERSPACE:
372                         output_userspace(dp, skb, a);
373                         break;
374
375                 case OVS_ACTION_ATTR_PUSH:
376                         /* Only supported push action is on vlan tag. */
377                         err = push_vlan(skb, nla_data(nla_data(a)));
378                         if (unlikely(err)) /* skb already freed. */
379                                 return err;
380                         break;
381
382                 case OVS_ACTION_ATTR_POP:
383                         /* Only supported pop action is on vlan tag. */
384                         err = pop_vlan(skb);
385                         break;
386
387                 case OVS_ACTION_ATTR_SET:
388                         err = execute_set_action(skb, nla_data(a));
389                         break;
390
391                 case OVS_ACTION_ATTR_SAMPLE:
392                         err = sample(dp, skb, a);
393                         break;
394                 }
395
396                 if (unlikely(err)) {
397                         kfree_skb(skb);
398                         return err;
399                 }
400         }
401
402         if (prev_port != -1) {
403                 if (keep_skb)
404                         skb = skb_clone(skb, GFP_ATOMIC);
405
406                 do_output(dp, skb, prev_port);
407         } else if (!keep_skb)
408                 consume_skb(skb);
409
410         return 0;
411 }
412
413 /* We limit the number of times that we pass into execute_actions()
414  * to avoid blowing out the stack in the event that we have a loop. */
415 #define MAX_LOOPS 5
416
417 struct loop_counter {
418         u8 count;               /* Count. */
419         bool looping;           /* Loop detected? */
420 };
421
422 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
423
424 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
425 {
426         if (net_ratelimit())
427                 pr_warn("%s: flow looped %d times, dropping\n",
428                                 dp_name(dp), MAX_LOOPS);
429         actions->actions_len = 0;
430         return -ELOOP;
431 }
432
433 /* Execute a list of actions against 'skb'. */
434 int execute_actions(struct datapath *dp, struct sk_buff *skb)
435 {
436         struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
437         struct loop_counter *loop;
438         int error;
439
440         /* Check whether we've looped too much. */
441         loop = &__get_cpu_var(loop_counters);
442         if (unlikely(++loop->count > MAX_LOOPS))
443                 loop->looping = true;
444         if (unlikely(loop->looping)) {
445                 error = loop_suppress(dp, acts);
446                 kfree_skb(skb);
447                 goto out_loop;
448         }
449
450         OVS_CB(skb)->tun_id = 0;
451         error = do_execute_actions(dp, skb, acts->actions,
452                                          acts->actions_len, false);
453
454         /* Check whether sub-actions looped too much. */
455         if (unlikely(loop->looping))
456                 error = loop_suppress(dp, acts);
457
458 out_loop:
459         /* Decrement loop counter. */
460         if (!--loop->count)
461                 loop->looping = false;
462
463         return error;
464 }