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