datapath: Use consume_skb() on non-errors.
[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 static int strip_vlan(struct sk_buff *skb)
43 {
44         struct ethhdr *eh;
45         int err;
46
47         if (vlan_tx_tag_present(skb)) {
48                 vlan_set_tci(skb, 0);
49                 return 0;
50         }
51
52         if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
53             skb->len < VLAN_ETH_HLEN))
54                 return 0;
55
56         err = make_writable(skb, VLAN_ETH_HLEN);
57         if (unlikely(err))
58                 return err;
59
60         if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
61                 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
62                                         + ETH_HLEN, VLAN_HLEN, 0));
63
64         memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
65
66         eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
67
68         skb->protocol = eh->h_proto;
69         skb->mac_header += VLAN_HLEN;
70
71         return 0;
72 }
73
74 static int modify_vlan_tci(struct sk_buff *skb, __be16 tci)
75 {
76         if (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_8021Q)) {
77                 int err;
78
79                 if (unlikely(skb->len < VLAN_ETH_HLEN))
80                         return 0;
81
82                 err = strip_vlan(skb);
83                 if (unlikely(err))
84                         return err;
85         }
86
87         __vlan_hwaccel_put_tag(skb, ntohs(tci));
88
89         return 0;
90 }
91
92 static bool is_ip(struct sk_buff *skb)
93 {
94         return (OVS_CB(skb)->flow->key.eth.type == htons(ETH_P_IP) &&
95                 skb->transport_header > skb->network_header);
96 }
97
98 static __sum16 *get_l4_checksum(struct sk_buff *skb)
99 {
100         u8 nw_proto = OVS_CB(skb)->flow->key.ip.proto;
101         int transport_len = skb->len - skb_transport_offset(skb);
102         if (nw_proto == IPPROTO_TCP) {
103                 if (likely(transport_len >= sizeof(struct tcphdr)))
104                         return &tcp_hdr(skb)->check;
105         } else if (nw_proto == IPPROTO_UDP) {
106                 if (likely(transport_len >= sizeof(struct udphdr)))
107                         return &udp_hdr(skb)->check;
108         }
109         return NULL;
110 }
111
112 static int set_nw_addr(struct sk_buff *skb, const struct nlattr *a)
113 {
114         __be32 new_nwaddr = nla_get_be32(a);
115         struct iphdr *nh;
116         __sum16 *check;
117         __be32 *nwaddr;
118         int err;
119
120         if (unlikely(!is_ip(skb)))
121                 return 0;
122
123         err = make_writable(skb, skb_network_offset(skb) +
124                                  sizeof(struct iphdr));
125         if (unlikely(err))
126                 return err;
127
128         nh = ip_hdr(skb);
129         nwaddr = nla_type(a) == ODP_ACTION_ATTR_SET_NW_SRC ? &nh->saddr : &nh->daddr;
130
131         check = get_l4_checksum(skb);
132         if (likely(check))
133                 inet_proto_csum_replace4(check, skb, *nwaddr, new_nwaddr, 1);
134         csum_replace4(&nh->check, *nwaddr, new_nwaddr);
135
136         skb_clear_rxhash(skb);
137
138         *nwaddr = new_nwaddr;
139
140         return 0;
141 }
142
143 static int set_nw_tos(struct sk_buff *skb, u8 nw_tos)
144 {
145         struct iphdr *nh = ip_hdr(skb);
146         u8 old, new;
147         int err;
148
149         if (unlikely(!is_ip(skb)))
150                 return 0;
151
152         err = make_writable(skb, skb_network_offset(skb) +
153                                  sizeof(struct iphdr));
154         if (unlikely(err))
155                 return err;
156
157         /* Set the DSCP bits and preserve the ECN bits. */
158         old = nh->tos;
159         new = nw_tos | (nh->tos & INET_ECN_MASK);
160         csum_replace4(&nh->check, (__force __be32)old,
161                                   (__force __be32)new);
162         nh->tos = new;
163
164         return 0;
165 }
166
167 static int set_tp_port(struct sk_buff *skb, const struct nlattr *a)
168 {
169         struct udphdr *th;
170         __sum16 *check;
171         __be16 *port;
172         int err;
173
174         if (unlikely(!is_ip(skb)))
175                 return 0;
176
177         err = make_writable(skb, skb_transport_offset(skb) +
178                                  sizeof(struct tcphdr));
179         if (unlikely(err))
180                 return err;
181
182         /* Must follow make_writable() since that can move the skb data. */
183         check = get_l4_checksum(skb);
184         if (unlikely(!check))
185                 return 0;
186
187         /*
188          * Update port and checksum.
189          *
190          * This is OK because source and destination port numbers are at the
191          * same offsets in both UDP and TCP headers, and get_l4_checksum() only
192          * supports those protocols.
193          */
194         th = udp_hdr(skb);
195         port = nla_type(a) == ODP_ACTION_ATTR_SET_TP_SRC ? &th->source : &th->dest;
196         inet_proto_csum_replace2(check, skb, *port, nla_get_be16(a), 0);
197         *port = nla_get_be16(a);
198         skb_clear_rxhash(skb);
199
200         return 0;
201 }
202
203 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
204 {
205         struct vport *p;
206
207         if (!skb)
208                 goto error;
209
210         p = rcu_dereference(dp->ports[out_port]);
211         if (!p)
212                 goto error;
213
214         vport_send(p, skb);
215         return;
216
217 error:
218         kfree_skb(skb);
219 }
220
221 static int output_control(struct datapath *dp, struct sk_buff *skb, u64 arg)
222 {
223         struct dp_upcall_info upcall;
224
225         skb = skb_clone(skb, GFP_ATOMIC);
226         if (!skb)
227                 return -ENOMEM;
228
229         upcall.cmd = ODP_PACKET_CMD_ACTION;
230         upcall.key = &OVS_CB(skb)->flow->key;
231         upcall.userdata = arg;
232         upcall.sample_pool = 0;
233         upcall.actions = NULL;
234         upcall.actions_len = 0;
235         return dp_upcall(dp, skb, &upcall);
236 }
237
238 /* Execute a list of actions against 'skb'. */
239 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
240                               struct sw_flow_actions *acts)
241 {
242         /* Every output action needs a separate clone of 'skb', but the common
243          * case is just a single output action, so that doing a clone and
244          * then freeing the original skbuff is wasteful.  So the following code
245          * is slightly obscure just to avoid that. */
246         int prev_port = -1;
247         u32 priority = skb->priority;
248         const struct nlattr *a;
249         int rem;
250
251         for (a = acts->actions, rem = acts->actions_len; rem > 0;
252              a = nla_next(a, &rem)) {
253                 int err = 0;
254
255                 if (prev_port != -1) {
256                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
257                         prev_port = -1;
258                 }
259
260                 switch (nla_type(a)) {
261                 case ODP_ACTION_ATTR_OUTPUT:
262                         prev_port = nla_get_u32(a);
263                         break;
264
265                 case ODP_ACTION_ATTR_CONTROLLER:
266                         err = output_control(dp, skb, nla_get_u64(a));
267                         break;
268
269                 case ODP_ACTION_ATTR_SET_TUNNEL:
270                         OVS_CB(skb)->tun_id = nla_get_be64(a);
271                         break;
272
273                 case ODP_ACTION_ATTR_SET_DL_TCI:
274                         err = modify_vlan_tci(skb, nla_get_be16(a));
275                         break;
276
277                 case ODP_ACTION_ATTR_STRIP_VLAN:
278                         err = strip_vlan(skb);
279                         break;
280
281                 case ODP_ACTION_ATTR_SET_DL_SRC:
282                         err = make_writable(skb, ETH_HLEN);
283                         if (likely(!err))
284                                 memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
285                         break;
286
287                 case ODP_ACTION_ATTR_SET_DL_DST:
288                         err = make_writable(skb, ETH_HLEN);
289                         if (likely(!err))
290                                 memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
291                         break;
292
293                 case ODP_ACTION_ATTR_SET_NW_SRC:
294                 case ODP_ACTION_ATTR_SET_NW_DST:
295                         err = set_nw_addr(skb, a);
296                         break;
297
298                 case ODP_ACTION_ATTR_SET_NW_TOS:
299                         err = set_nw_tos(skb, nla_get_u8(a));
300                         break;
301
302                 case ODP_ACTION_ATTR_SET_TP_SRC:
303                 case ODP_ACTION_ATTR_SET_TP_DST:
304                         err = set_tp_port(skb, a);
305                         break;
306
307                 case ODP_ACTION_ATTR_SET_PRIORITY:
308                         skb->priority = nla_get_u32(a);
309                         break;
310
311                 case ODP_ACTION_ATTR_POP_PRIORITY:
312                         skb->priority = priority;
313                         break;
314                 }
315
316                 if (unlikely(err)) {
317                         kfree_skb(skb);
318                         return err;
319                 }
320         }
321
322         if (prev_port != -1)
323                 do_output(dp, skb, prev_port);
324         else
325                 consume_skb(skb);
326
327         return 0;
328 }
329
330 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
331                          struct sw_flow_actions *acts)
332 {
333         struct sk_buff *nskb;
334         struct vport *p = OVS_CB(skb)->vport;
335         struct dp_upcall_info upcall;
336
337         if (unlikely(!p))
338                 return;
339
340         atomic_inc(&p->sflow_pool);
341         if (net_random() >= dp->sflow_probability)
342                 return;
343
344         nskb = skb_clone(skb, GFP_ATOMIC);
345         if (unlikely(!nskb))
346                 return;
347
348         upcall.cmd = ODP_PACKET_CMD_SAMPLE;
349         upcall.key = &OVS_CB(skb)->flow->key;
350         upcall.userdata = 0;
351         upcall.sample_pool = atomic_read(&p->sflow_pool);
352         upcall.actions = acts->actions;
353         upcall.actions_len = acts->actions_len;
354         dp_upcall(dp, nskb, &upcall);
355 }
356
357 /* Execute a list of actions against 'skb'. */
358 int execute_actions(struct datapath *dp, struct sk_buff *skb)
359 {
360         struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
361         struct loop_counter *loop;
362         int error;
363
364         /* Check whether we've looped too much. */
365         loop = loop_get_counter();
366         if (unlikely(++loop->count > MAX_LOOPS))
367                 loop->looping = true;
368         if (unlikely(loop->looping)) {
369                 error = loop_suppress(dp, acts);
370                 kfree_skb(skb);
371                 goto out_loop;
372         }
373
374         /* Really execute actions. */
375         if (dp->sflow_probability)
376                 sflow_sample(dp, skb, acts);
377         OVS_CB(skb)->tun_id = 0;
378         error = do_execute_actions(dp, skb, acts);
379
380         /* Check whether sub-actions looped too much. */
381         if (unlikely(loop->looping))
382                 error = loop_suppress(dp, acts);
383
384 out_loop:
385         /* Decrement loop counter. */
386         if (!--loop->count)
387                 loop->looping = false;
388         loop_put_counter();
389
390         return error;
391 }