datapath: Send to userspace errors shouldn't halt processing.
[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 #include <linux/skbuff.h>
12 #include <linux/in.h>
13 #include <linux/ip.h>
14 #include <linux/tcp.h>
15 #include <linux/udp.h>
16 #include <linux/in6.h>
17 #include <linux/if_arp.h>
18 #include <linux/if_vlan.h>
19 #include <net/inet_ecn.h>
20 #include <net/ip.h>
21 #include <net/checksum.h>
22
23 #include "actions.h"
24 #include "checksum.h"
25 #include "datapath.h"
26 #include "loop_counter.h"
27 #include "openvswitch/datapath-protocol.h"
28 #include "vlan.h"
29 #include "vport.h"
30
31 static int do_execute_actions(struct datapath *, struct sk_buff *,
32                               struct sw_flow_actions *acts);
33
34 static int make_writable(struct sk_buff *skb, int write_len)
35 {
36         if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
37                 return 0;
38
39         return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
40 }
41
42 /* remove VLAN header from packet and update csum accrodingly. */
43 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
44 {
45         struct ethhdr *eh;
46         struct vlan_ethhdr *veth;
47         int err;
48
49         err = make_writable(skb, VLAN_ETH_HLEN);
50         if (unlikely(err))
51                 return err;
52
53         if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
54                 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
55                                         + ETH_HLEN, VLAN_HLEN, 0));
56
57         veth = (struct vlan_ethhdr *) skb->data;
58         *current_tci = veth->h_vlan_TCI;
59
60         memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
61
62         eh = (struct ethhdr *)__skb_pull(skb, VLAN_HLEN);
63
64         skb->protocol = eh->h_proto;
65         skb->mac_header += VLAN_HLEN;
66
67         return 0;
68 }
69
70 static int pop_vlan(struct sk_buff *skb)
71 {
72         __be16 tci;
73         int err;
74
75         if (likely(vlan_tx_tag_present(skb))) {
76                 vlan_set_tci(skb, 0);
77         } else {
78                 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
79                     skb->len < VLAN_ETH_HLEN))
80                         return 0;
81
82                 err = __pop_vlan_tci(skb, &tci);
83                 if (err)
84                         return err;
85         }
86         /* move next vlan tag to hw accel tag */
87         if (likely(skb->protocol != htons(ETH_P_8021Q) ||
88             skb->len < VLAN_ETH_HLEN))
89                 return 0;
90
91         err = __pop_vlan_tci(skb, &tci);
92         if (unlikely(err))
93                 return err;
94
95         __vlan_hwaccel_put_tag(skb, ntohs(tci));
96         return 0;
97 }
98
99 static int push_vlan(struct sk_buff *skb, __be16 new_tci)
100 {
101         if (unlikely(vlan_tx_tag_present(skb))) {
102                 u16 current_tag;
103
104                 /* push down current VLAN tag */
105                 current_tag = vlan_tx_tag_get(skb);
106
107                 if (!__vlan_put_tag(skb, current_tag))
108                         return -ENOMEM;
109
110                 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
111                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
112                                         + ETH_HLEN, VLAN_HLEN, 0));
113
114         }
115         __vlan_hwaccel_put_tag(skb, ntohs(new_tci));
116         return 0;
117 }
118
119 static bool is_ip(struct sk_buff *skb)
120 {
121         return (OVS_CB(skb)->flow->key.eth.type == htons(ETH_P_IP) &&
122                 skb->transport_header > skb->network_header);
123 }
124
125 static __sum16 *get_l4_checksum(struct sk_buff *skb)
126 {
127         u8 nw_proto = OVS_CB(skb)->flow->key.ip.proto;
128         int transport_len = skb->len - skb_transport_offset(skb);
129         if (nw_proto == IPPROTO_TCP) {
130                 if (likely(transport_len >= sizeof(struct tcphdr)))
131                         return &tcp_hdr(skb)->check;
132         } else if (nw_proto == IPPROTO_UDP) {
133                 if (likely(transport_len >= sizeof(struct udphdr)))
134                         return &udp_hdr(skb)->check;
135         }
136         return NULL;
137 }
138
139 static int set_nw_addr(struct sk_buff *skb, const struct nlattr *a)
140 {
141         __be32 new_nwaddr = nla_get_be32(a);
142         struct iphdr *nh;
143         __sum16 *check;
144         __be32 *nwaddr;
145         int err;
146
147         if (unlikely(!is_ip(skb)))
148                 return 0;
149
150         err = make_writable(skb, skb_network_offset(skb) +
151                                  sizeof(struct iphdr));
152         if (unlikely(err))
153                 return err;
154
155         nh = ip_hdr(skb);
156         nwaddr = nla_type(a) == OVS_ACTION_ATTR_SET_NW_SRC ? &nh->saddr : &nh->daddr;
157
158         check = get_l4_checksum(skb);
159         if (likely(check))
160                 inet_proto_csum_replace4(check, skb, *nwaddr, new_nwaddr, 1);
161         csum_replace4(&nh->check, *nwaddr, new_nwaddr);
162
163         skb_clear_rxhash(skb);
164
165         *nwaddr = new_nwaddr;
166
167         return 0;
168 }
169
170 static int set_nw_tos(struct sk_buff *skb, u8 nw_tos)
171 {
172         struct iphdr *nh = ip_hdr(skb);
173         u8 old, new;
174         int err;
175
176         if (unlikely(!is_ip(skb)))
177                 return 0;
178
179         err = make_writable(skb, skb_network_offset(skb) +
180                                  sizeof(struct iphdr));
181         if (unlikely(err))
182                 return err;
183
184         /* Set the DSCP bits and preserve the ECN bits. */
185         old = nh->tos;
186         new = nw_tos | (nh->tos & INET_ECN_MASK);
187         csum_replace4(&nh->check, (__force __be32)old,
188                                   (__force __be32)new);
189         nh->tos = new;
190
191         return 0;
192 }
193
194 static int set_tp_port(struct sk_buff *skb, const struct nlattr *a)
195 {
196         struct udphdr *th;
197         __sum16 *check;
198         __be16 *port;
199         int err;
200
201         if (unlikely(!is_ip(skb)))
202                 return 0;
203
204         err = make_writable(skb, skb_transport_offset(skb) +
205                                  sizeof(struct tcphdr));
206         if (unlikely(err))
207                 return err;
208
209         /* Must follow make_writable() since that can move the skb data. */
210         check = get_l4_checksum(skb);
211         if (unlikely(!check))
212                 return 0;
213
214         /*
215          * Update port and checksum.
216          *
217          * This is OK because source and destination port numbers are at the
218          * same offsets in both UDP and TCP headers, and get_l4_checksum() only
219          * supports those protocols.
220          */
221         th = udp_hdr(skb);
222         port = nla_type(a) == OVS_ACTION_ATTR_SET_TP_SRC ? &th->source : &th->dest;
223         inet_proto_csum_replace2(check, skb, *port, nla_get_be16(a), 0);
224         *port = nla_get_be16(a);
225         skb_clear_rxhash(skb);
226
227         return 0;
228 }
229
230 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
231 {
232         struct vport *p;
233
234         if (!skb)
235                 goto error;
236
237         p = rcu_dereference(dp->ports[out_port]);
238         if (!p)
239                 goto error;
240
241         vport_send(p, skb);
242         return;
243
244 error:
245         kfree_skb(skb);
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         skb = skb_clone(skb, GFP_ATOMIC);
253         if (!skb)
254                 return -ENOMEM;
255
256         upcall.cmd = OVS_PACKET_CMD_ACTION;
257         upcall.key = &OVS_CB(skb)->flow->key;
258         upcall.userdata = arg;
259         upcall.sample_pool = 0;
260         upcall.actions = NULL;
261         upcall.actions_len = 0;
262         return dp_upcall(dp, skb, &upcall);
263 }
264
265 /* Execute a list of actions against 'skb'. */
266 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
267                               struct sw_flow_actions *acts)
268 {
269         /* Every output action needs a separate clone of 'skb', but the common
270          * case is just a single output action, so that doing a clone and
271          * then freeing the original skbuff is wasteful.  So the following code
272          * is slightly obscure just to avoid that. */
273         int prev_port = -1;
274         u32 priority = skb->priority;
275         const struct nlattr *a;
276         int rem;
277
278         for (a = acts->actions, rem = acts->actions_len; rem > 0;
279              a = nla_next(a, &rem)) {
280                 int err = 0;
281
282                 if (prev_port != -1) {
283                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
284                         prev_port = -1;
285                 }
286
287                 switch (nla_type(a)) {
288                 case OVS_ACTION_ATTR_OUTPUT:
289                         prev_port = nla_get_u32(a);
290                         break;
291
292                 case OVS_ACTION_ATTR_USERSPACE:
293                         output_userspace(dp, skb, nla_get_u64(a));
294                         break;
295
296                 case OVS_ACTION_ATTR_SET_TUNNEL:
297                         OVS_CB(skb)->tun_id = nla_get_be64(a);
298                         break;
299
300                 case OVS_ACTION_ATTR_PUSH_VLAN:
301                         err = push_vlan(skb, nla_get_be16(a));
302                         if (unlikely(err)) /* skb already freed */
303                                 return err;
304                         break;
305
306                 case OVS_ACTION_ATTR_POP_VLAN:
307                         err = pop_vlan(skb);
308                         break;
309
310                 case OVS_ACTION_ATTR_SET_DL_SRC:
311                         err = make_writable(skb, ETH_HLEN);
312                         if (likely(!err))
313                                 memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
314                         break;
315
316                 case OVS_ACTION_ATTR_SET_DL_DST:
317                         err = make_writable(skb, ETH_HLEN);
318                         if (likely(!err))
319                                 memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
320                         break;
321
322                 case OVS_ACTION_ATTR_SET_NW_SRC:
323                 case OVS_ACTION_ATTR_SET_NW_DST:
324                         err = set_nw_addr(skb, a);
325                         break;
326
327                 case OVS_ACTION_ATTR_SET_NW_TOS:
328                         err = set_nw_tos(skb, nla_get_u8(a));
329                         break;
330
331                 case OVS_ACTION_ATTR_SET_TP_SRC:
332                 case OVS_ACTION_ATTR_SET_TP_DST:
333                         err = set_tp_port(skb, a);
334                         break;
335
336                 case OVS_ACTION_ATTR_SET_PRIORITY:
337                         skb->priority = nla_get_u32(a);
338                         break;
339
340                 case OVS_ACTION_ATTR_POP_PRIORITY:
341                         skb->priority = priority;
342                         break;
343                 }
344
345                 if (unlikely(err)) {
346                         kfree_skb(skb);
347                         return err;
348                 }
349         }
350
351         if (prev_port != -1)
352                 do_output(dp, skb, prev_port);
353         else
354                 consume_skb(skb);
355
356         return 0;
357 }
358
359 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
360                          struct sw_flow_actions *acts)
361 {
362         struct sk_buff *nskb;
363         struct vport *p = OVS_CB(skb)->vport;
364         struct dp_upcall_info upcall;
365
366         if (unlikely(!p))
367                 return;
368
369         atomic_inc(&p->sflow_pool);
370         if (net_random() >= dp->sflow_probability)
371                 return;
372
373         nskb = skb_clone(skb, GFP_ATOMIC);
374         if (unlikely(!nskb))
375                 return;
376
377         upcall.cmd = OVS_PACKET_CMD_SAMPLE;
378         upcall.key = &OVS_CB(skb)->flow->key;
379         upcall.userdata = 0;
380         upcall.sample_pool = atomic_read(&p->sflow_pool);
381         upcall.actions = acts->actions;
382         upcall.actions_len = acts->actions_len;
383         dp_upcall(dp, nskb, &upcall);
384 }
385
386 /* Execute a list of actions against 'skb'. */
387 int execute_actions(struct datapath *dp, struct sk_buff *skb)
388 {
389         struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
390         struct loop_counter *loop;
391         int error;
392
393         /* Check whether we've looped too much. */
394         loop = loop_get_counter();
395         if (unlikely(++loop->count > MAX_LOOPS))
396                 loop->looping = true;
397         if (unlikely(loop->looping)) {
398                 error = loop_suppress(dp, acts);
399                 kfree_skb(skb);
400                 goto out_loop;
401         }
402
403         /* Really execute actions. */
404         if (dp->sflow_probability)
405                 sflow_sample(dp, skb, acts);
406         OVS_CB(skb)->tun_id = 0;
407         error = do_execute_actions(dp, skb, acts);
408
409         /* Check whether sub-actions looped too much. */
410         if (unlikely(loop->looping))
411                 error = loop_suppress(dp, acts);
412
413 out_loop:
414         /* Decrement loop counter. */
415         if (!--loop->count)
416                 loop->looping = false;
417         loop_put_counter();
418
419         return error;
420 }