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