datapath: Remove RT kernel support.
[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/tcp.h>
17 #include <linux/udp.h>
18 #include <linux/in6.h>
19 #include <linux/if_arp.h>
20 #include <linux/if_vlan.h>
21 #include <net/inet_ecn.h>
22 #include <net/ip.h>
23 #include <net/checksum.h>
24
25 #include "actions.h"
26 #include "checksum.h"
27 #include "datapath.h"
28 #include "openvswitch/datapath-protocol.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, __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;
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(new_tci));
117         return 0;
118 }
119
120 static bool is_ip(struct sk_buff *skb)
121 {
122         return (OVS_CB(skb)->flow->key.eth.type == htons(ETH_P_IP) &&
123                 skb->transport_header > skb->network_header);
124 }
125
126 static __sum16 *get_l4_checksum(struct sk_buff *skb)
127 {
128         u8 nw_proto = OVS_CB(skb)->flow->key.ip.proto;
129         int transport_len = skb->len - skb_transport_offset(skb);
130         if (nw_proto == IPPROTO_TCP) {
131                 if (likely(transport_len >= sizeof(struct tcphdr)))
132                         return &tcp_hdr(skb)->check;
133         } else if (nw_proto == IPPROTO_UDP) {
134                 if (likely(transport_len >= sizeof(struct udphdr)))
135                         return &udp_hdr(skb)->check;
136         }
137         return NULL;
138 }
139
140 static int set_nw_addr(struct sk_buff *skb, const struct nlattr *a)
141 {
142         __be32 new_nwaddr = nla_get_be32(a);
143         struct iphdr *nh;
144         __sum16 *check;
145         __be32 *nwaddr;
146         int err;
147
148         if (unlikely(!is_ip(skb)))
149                 return 0;
150
151         err = make_writable(skb, skb_network_offset(skb) +
152                                  sizeof(struct iphdr));
153         if (unlikely(err))
154                 return err;
155
156         nh = ip_hdr(skb);
157         nwaddr = nla_type(a) == OVS_ACTION_ATTR_SET_NW_SRC ? &nh->saddr : &nh->daddr;
158
159         check = get_l4_checksum(skb);
160         if (likely(check))
161                 inet_proto_csum_replace4(check, skb, *nwaddr, new_nwaddr, 1);
162         csum_replace4(&nh->check, *nwaddr, new_nwaddr);
163
164         skb_clear_rxhash(skb);
165
166         *nwaddr = new_nwaddr;
167
168         return 0;
169 }
170
171 static int set_nw_tos(struct sk_buff *skb, u8 nw_tos)
172 {
173         struct iphdr *nh = ip_hdr(skb);
174         u8 old, new;
175         int err;
176
177         if (unlikely(!is_ip(skb)))
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;
193 }
194
195 static int set_tp_port(struct sk_buff *skb, const struct nlattr *a)
196 {
197         struct udphdr *th;
198         __sum16 *check;
199         __be16 *port;
200         int err;
201
202         if (unlikely(!is_ip(skb)))
203                 return 0;
204
205         err = make_writable(skb, skb_transport_offset(skb) +
206                                  sizeof(struct tcphdr));
207         if (unlikely(err))
208                 return err;
209
210         /* Must follow make_writable() since that can move the skb data. */
211         check = get_l4_checksum(skb);
212         if (unlikely(!check))
213                 return 0;
214
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);
223         port = nla_type(a) == OVS_ACTION_ATTR_SET_TP_SRC ? &th->source : &th->dest;
224         inet_proto_csum_replace2(check, skb, *port, nla_get_be16(a), 0);
225         *port = nla_get_be16(a);
226         skb_clear_rxhash(skb);
227
228         return 0;
229 }
230
231 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
232 {
233         struct vport *vport;
234
235         if (unlikely(!skb))
236                 return -ENOMEM;
237
238         vport = rcu_dereference(dp->ports[out_port]);
239         if (unlikely(!vport)) {
240                 kfree_skb(skb);
241                 return -ENODEV;
242         }
243
244         vport_send(vport, skb);
245         return 0;
246 }
247
248 static int output_userspace(struct datapath *dp, struct sk_buff *skb, u64 arg)
249 {
250         struct dp_upcall_info upcall;
251
252         upcall.cmd = OVS_PACKET_CMD_ACTION;
253         upcall.key = &OVS_CB(skb)->flow->key;
254         upcall.userdata = arg;
255         return dp_upcall(dp, skb, &upcall);
256 }
257
258 static 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
283 /* Execute a list of actions against 'skb'. */
284 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
285                         const struct nlattr *attr, int len, bool keep_skb)
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;
292         u32 priority = skb->priority;
293         const struct nlattr *a;
294         int rem;
295
296         for (a = attr, rem = len; rem > 0;
297              a = nla_next(a, &rem)) {
298                 int err = 0;
299
300                 if (prev_port != -1) {
301                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
302                         prev_port = -1;
303                 }
304
305                 switch (nla_type(a)) {
306                 case OVS_ACTION_ATTR_OUTPUT:
307                         prev_port = nla_get_u32(a);
308                         break;
309
310                 case OVS_ACTION_ATTR_USERSPACE:
311                         output_userspace(dp, skb, nla_get_u64(a));
312                         break;
313
314                 case OVS_ACTION_ATTR_SET_TUNNEL:
315                         OVS_CB(skb)->tun_id = nla_get_be64(a);
316                         break;
317
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;
322                         break;
323
324                 case OVS_ACTION_ATTR_POP_VLAN:
325                         err = pop_vlan(skb);
326                         break;
327
328                 case OVS_ACTION_ATTR_SET_DL_SRC:
329                         err = make_writable(skb, ETH_HLEN);
330                         if (likely(!err))
331                                 memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
332                         break;
333
334                 case OVS_ACTION_ATTR_SET_DL_DST:
335                         err = make_writable(skb, ETH_HLEN);
336                         if (likely(!err))
337                                 memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
338                         break;
339
340                 case OVS_ACTION_ATTR_SET_NW_SRC:
341                 case OVS_ACTION_ATTR_SET_NW_DST:
342                         err = set_nw_addr(skb, a);
343                         break;
344
345                 case OVS_ACTION_ATTR_SET_NW_TOS:
346                         err = set_nw_tos(skb, nla_get_u8(a));
347                         break;
348
349                 case OVS_ACTION_ATTR_SET_TP_SRC:
350                 case OVS_ACTION_ATTR_SET_TP_DST:
351                         err = set_tp_port(skb, a);
352                         break;
353
354                 case OVS_ACTION_ATTR_SET_PRIORITY:
355                         skb->priority = nla_get_u32(a);
356                         break;
357
358                 case OVS_ACTION_ATTR_POP_PRIORITY:
359                         skb->priority = priority;
360                         break;
361
362                 case OVS_ACTION_ATTR_SAMPLE:
363                         err = sample(dp, skb, a);
364                         break;
365
366                 }
367                 if (unlikely(err)) {
368                         kfree_skb(skb);
369                         return err;
370                 }
371         }
372
373         if (prev_port != -1) {
374                 if (keep_skb)
375                         skb = skb_clone(skb, GFP_ATOMIC);
376
377                 do_output(dp, skb, prev_port);
378         } else if (!keep_skb)
379                 consume_skb(skb);
380
381         return 0;
382 }
383
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
388 struct loop_counter {
389         u8 count;               /* Count. */
390         bool looping;           /* Loop detected? */
391 };
392
393 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
394
395 static 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
404 /* Execute a list of actions against 'skb'. */
405 int execute_actions(struct datapath *dp, struct sk_buff *skb)
406 {
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. */
412         loop = &__get_cpu_var(loop_counters);
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         }
420
421         OVS_CB(skb)->tun_id = 0;
422         error = do_execute_actions(dp, skb, acts->actions,
423                                          acts->actions_len, false);
424
425         /* Check whether sub-actions looped too much. */
426         if (unlikely(loop->looping))
427                 error = loop_suppress(dp, acts);
428
429 out_loop:
430         /* Decrement loop counter. */
431         if (!--loop->count)
432                 loop->looping = false;
433
434         return error;
435 }