datapath: Avoid skb-clone in upcall
[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 *dp, struct sk_buff *skb,
32                         const struct nlattr *attr, int len, bool keep_skb);
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 int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
231 {
232         struct vport *vport;
233
234         if (unlikely(!skb))
235                 return -ENOMEM;
236
237         vport = rcu_dereference(dp->ports[out_port]);
238         if (unlikely(!vport)) {
239                 kfree_skb(skb);
240                 return -ENODEV;
241         }
242
243         vport_send(vport, skb);
244         return 0;
245 }
246
247 static int output_userspace(struct datapath *dp, struct sk_buff *skb, u64 arg)
248 {
249         struct dp_upcall_info upcall;
250
251         upcall.cmd = OVS_PACKET_CMD_ACTION;
252         upcall.key = &OVS_CB(skb)->flow->key;
253         upcall.userdata = arg;
254         return dp_upcall(dp, skb, &upcall);
255 }
256
257 static int sample(struct datapath *dp, struct sk_buff *skb,
258                   const struct nlattr *attr)
259 {
260         const struct nlattr *acts_list = NULL;
261         const struct nlattr *a;
262         int rem;
263
264         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
265                  a = nla_next(a, &rem)) {
266                 switch (nla_type(a)) {
267                 case OVS_SAMPLE_ATTR_PROBABILITY:
268                         if (net_random() >= nla_get_u32(a))
269                                 return 0;
270                         break;
271
272                 case OVS_SAMPLE_ATTR_ACTIONS:
273                         acts_list = a;
274                         break;
275                 }
276         }
277
278         return do_execute_actions(dp, skb, nla_data(acts_list),
279                                                  nla_len(acts_list), true);
280 }
281
282 /* Execute a list of actions against 'skb'. */
283 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
284                         const struct nlattr *attr, int len, bool keep_skb)
285 {
286         /* Every output action needs a separate clone of 'skb', but the common
287          * case is just a single output action, so that doing a clone and
288          * then freeing the original skbuff is wasteful.  So the following code
289          * is slightly obscure just to avoid that. */
290         int prev_port = -1;
291         u32 priority = skb->priority;
292         const struct nlattr *a;
293         int rem;
294
295         for (a = attr, rem = len; rem > 0;
296              a = nla_next(a, &rem)) {
297                 int err = 0;
298
299                 if (prev_port != -1) {
300                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
301                         prev_port = -1;
302                 }
303
304                 switch (nla_type(a)) {
305                 case OVS_ACTION_ATTR_OUTPUT:
306                         prev_port = nla_get_u32(a);
307                         break;
308
309                 case OVS_ACTION_ATTR_USERSPACE:
310                         output_userspace(dp, skb, nla_get_u64(a));
311                         break;
312
313                 case OVS_ACTION_ATTR_SET_TUNNEL:
314                         OVS_CB(skb)->tun_id = nla_get_be64(a);
315                         break;
316
317                 case OVS_ACTION_ATTR_PUSH_VLAN:
318                         err = push_vlan(skb, nla_get_be16(a));
319                         if (unlikely(err)) /* skb already freed */
320                                 return err;
321                         break;
322
323                 case OVS_ACTION_ATTR_POP_VLAN:
324                         err = pop_vlan(skb);
325                         break;
326
327                 case OVS_ACTION_ATTR_SET_DL_SRC:
328                         err = make_writable(skb, ETH_HLEN);
329                         if (likely(!err))
330                                 memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
331                         break;
332
333                 case OVS_ACTION_ATTR_SET_DL_DST:
334                         err = make_writable(skb, ETH_HLEN);
335                         if (likely(!err))
336                                 memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
337                         break;
338
339                 case OVS_ACTION_ATTR_SET_NW_SRC:
340                 case OVS_ACTION_ATTR_SET_NW_DST:
341                         err = set_nw_addr(skb, a);
342                         break;
343
344                 case OVS_ACTION_ATTR_SET_NW_TOS:
345                         err = set_nw_tos(skb, nla_get_u8(a));
346                         break;
347
348                 case OVS_ACTION_ATTR_SET_TP_SRC:
349                 case OVS_ACTION_ATTR_SET_TP_DST:
350                         err = set_tp_port(skb, a);
351                         break;
352
353                 case OVS_ACTION_ATTR_SET_PRIORITY:
354                         skb->priority = nla_get_u32(a);
355                         break;
356
357                 case OVS_ACTION_ATTR_POP_PRIORITY:
358                         skb->priority = priority;
359                         break;
360
361                 case OVS_ACTION_ATTR_SAMPLE:
362                         err = sample(dp, skb, a);
363                         break;
364
365                 }
366                 if (unlikely(err)) {
367                         kfree_skb(skb);
368                         return err;
369                 }
370         }
371
372         if (prev_port != -1) {
373                 if (keep_skb)
374                         skb = skb_clone(skb, GFP_ATOMIC);
375
376                 do_output(dp, skb, prev_port);
377         } else if (!keep_skb)
378                 consume_skb(skb);
379
380         return 0;
381 }
382
383 /* Execute a list of actions against 'skb'. */
384 int execute_actions(struct datapath *dp, struct sk_buff *skb)
385 {
386         struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
387         struct loop_counter *loop;
388         int error;
389
390         /* Check whether we've looped too much. */
391         loop = loop_get_counter();
392         if (unlikely(++loop->count > MAX_LOOPS))
393                 loop->looping = true;
394         if (unlikely(loop->looping)) {
395                 error = loop_suppress(dp, acts);
396                 kfree_skb(skb);
397                 goto out_loop;
398         }
399
400         OVS_CB(skb)->tun_id = 0;
401         error = do_execute_actions(dp, skb, acts->actions,
402                                          acts->actions_len, false);
403
404         /* Check whether sub-actions looped too much. */
405         if (unlikely(loop->looping))
406                 error = loop_suppress(dp, acts);
407
408 out_loop:
409         /* Decrement loop counter. */
410         if (!--loop->count)
411                 loop->looping = false;
412         loop_put_counter();
413
414         return error;
415 }