datapath: Reformat copyright messages.
[sliver-openvswitch.git] / datapath / actions.c
1 /*
2  * Copyright (c) 2007-2011 Nicira Networks.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/skbuff.h>
22 #include <linux/in.h>
23 #include <linux/ip.h>
24 #include <linux/openvswitch.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/in6.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_vlan.h>
30 #include <net/ip.h>
31 #include <net/checksum.h>
32 #include <net/dsfield.h>
33
34 #include "checksum.h"
35 #include "datapath.h"
36 #include "vlan.h"
37 #include "vport.h"
38
39 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
40                         const struct nlattr *attr, int len, bool keep_skb);
41
42 static int make_writable(struct sk_buff *skb, int write_len)
43 {
44         if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
45                 return 0;
46
47         return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
48 }
49
50 /* remove VLAN header from packet and update csum accrodingly. */
51 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
52 {
53         struct ethhdr *eh;
54         struct vlan_ethhdr *veth;
55         int err;
56
57         err = make_writable(skb, VLAN_ETH_HLEN);
58         if (unlikely(err))
59                 return err;
60
61         if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
62                 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
63                                         + ETH_HLEN, VLAN_HLEN, 0));
64
65         veth = (struct vlan_ethhdr *) skb->data;
66         *current_tci = veth->h_vlan_TCI;
67
68         memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
69
70         eh = (struct ethhdr *)__skb_pull(skb, VLAN_HLEN);
71
72         skb->protocol = eh->h_proto;
73         skb->mac_header += VLAN_HLEN;
74
75         return 0;
76 }
77
78 static int pop_vlan(struct sk_buff *skb)
79 {
80         __be16 tci;
81         int err;
82
83         if (likely(vlan_tx_tag_present(skb))) {
84                 vlan_set_tci(skb, 0);
85         } else {
86                 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
87                              skb->len < VLAN_ETH_HLEN))
88                         return 0;
89
90                 err = __pop_vlan_tci(skb, &tci);
91                 if (err)
92                         return err;
93         }
94         /* move next vlan tag to hw accel tag */
95         if (likely(skb->protocol != htons(ETH_P_8021Q) ||
96                    skb->len < VLAN_ETH_HLEN))
97                 return 0;
98
99         err = __pop_vlan_tci(skb, &tci);
100         if (unlikely(err))
101                 return err;
102
103         __vlan_hwaccel_put_tag(skb, ntohs(tci));
104         return 0;
105 }
106
107 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
108 {
109         if (unlikely(vlan_tx_tag_present(skb))) {
110                 u16 current_tag;
111
112                 /* push down current VLAN tag */
113                 current_tag = vlan_tx_tag_get(skb);
114
115                 if (!__vlan_put_tag(skb, current_tag))
116                         return -ENOMEM;
117
118                 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
119                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
120                                         + ETH_HLEN, VLAN_HLEN, 0));
121
122         }
123         __vlan_hwaccel_put_tag(skb, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
124         return 0;
125 }
126
127 static int set_eth_addr(struct sk_buff *skb,
128                         const struct ovs_key_ethernet *eth_key)
129 {
130         int err;
131         err = make_writable(skb, ETH_HLEN);
132         if (unlikely(err))
133                 return err;
134
135         memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN);
136         memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN);
137
138         return 0;
139 }
140
141 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
142                                 __be32 *addr, __be32 new_addr)
143 {
144         int transport_len = skb->len - skb_transport_offset(skb);
145
146         if (nh->protocol == IPPROTO_TCP) {
147                 if (likely(transport_len >= sizeof(struct tcphdr)))
148                         inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
149                                                  *addr, new_addr, 1);
150         } else if (nh->protocol == IPPROTO_UDP) {
151                 if (likely(transport_len >= sizeof(struct udphdr)))
152                         inet_proto_csum_replace4(&udp_hdr(skb)->check, skb,
153                                                  *addr, new_addr, 1);
154         }
155
156         csum_replace4(&nh->check, *addr, new_addr);
157         skb_clear_rxhash(skb);
158         *addr = new_addr;
159 }
160
161 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
162 {
163         csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
164         nh->ttl = new_ttl;
165 }
166
167 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
168 {
169         struct iphdr *nh;
170         int err;
171
172         err = make_writable(skb, skb_network_offset(skb) +
173                                  sizeof(struct iphdr));
174         if (unlikely(err))
175                 return err;
176
177         nh = ip_hdr(skb);
178
179         if (ipv4_key->ipv4_src != nh->saddr)
180                 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
181
182         if (ipv4_key->ipv4_dst != nh->daddr)
183                 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
184
185         if (ipv4_key->ipv4_tos != nh->tos)
186                 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
187
188         if (ipv4_key->ipv4_ttl != nh->ttl)
189                 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
190
191         return 0;
192 }
193
194 /* Must follow make_writable() since that can move the skb data. */
195 static void set_tp_port(struct sk_buff *skb, __be16 *port,
196                          __be16 new_port, __sum16 *check)
197 {
198         inet_proto_csum_replace2(check, skb, *port, new_port, 0);
199         *port = new_port;
200         skb_clear_rxhash(skb);
201 }
202
203 static int set_udp_port(struct sk_buff *skb,
204                         const struct ovs_key_udp *udp_port_key)
205 {
206         struct udphdr *uh;
207         int err;
208
209         err = make_writable(skb, skb_transport_offset(skb) +
210                                  sizeof(struct udphdr));
211         if (unlikely(err))
212                 return err;
213
214         uh = udp_hdr(skb);
215         if (udp_port_key->udp_src != uh->source)
216                 set_tp_port(skb, &uh->source, udp_port_key->udp_src, &uh->check);
217
218         if (udp_port_key->udp_dst != uh->dest)
219                 set_tp_port(skb, &uh->dest, udp_port_key->udp_dst, &uh->check);
220
221         return 0;
222 }
223
224 static int set_tcp_port(struct sk_buff *skb,
225                         const struct ovs_key_tcp *tcp_port_key)
226 {
227         struct tcphdr *th;
228         int err;
229
230         err = make_writable(skb, skb_transport_offset(skb) +
231                                  sizeof(struct tcphdr));
232         if (unlikely(err))
233                 return err;
234
235         th = tcp_hdr(skb);
236         if (tcp_port_key->tcp_src != th->source)
237                 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
238
239         if (tcp_port_key->tcp_dst != th->dest)
240                 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
241
242         return 0;
243 }
244
245 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
246 {
247         struct vport *vport;
248
249         if (unlikely(!skb))
250                 return -ENOMEM;
251
252         vport = rcu_dereference(dp->ports[out_port]);
253         if (unlikely(!vport)) {
254                 kfree_skb(skb);
255                 return -ENODEV;
256         }
257
258         vport_send(vport, skb);
259         return 0;
260 }
261
262 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
263                             const struct nlattr *attr)
264 {
265         struct dp_upcall_info upcall;
266         const struct nlattr *a;
267         int rem;
268
269         upcall.cmd = OVS_PACKET_CMD_ACTION;
270         upcall.key = &OVS_CB(skb)->flow->key;
271         upcall.userdata = NULL;
272         upcall.pid = 0;
273
274         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
275                  a = nla_next(a, &rem)) {
276                 switch (nla_type(a)) {
277                 case OVS_USERSPACE_ATTR_USERDATA:
278                         upcall.userdata = a;
279                         break;
280
281                 case OVS_USERSPACE_ATTR_PID:
282                         upcall.pid = nla_get_u32(a);
283                         break;
284                 }
285         }
286
287         return dp_upcall(dp, skb, &upcall);
288 }
289
290 static int sample(struct datapath *dp, struct sk_buff *skb,
291                   const struct nlattr *attr)
292 {
293         const struct nlattr *acts_list = NULL;
294         const struct nlattr *a;
295         int rem;
296
297         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
298                  a = nla_next(a, &rem)) {
299                 switch (nla_type(a)) {
300                 case OVS_SAMPLE_ATTR_PROBABILITY:
301                         if (net_random() >= nla_get_u32(a))
302                                 return 0;
303                         break;
304
305                 case OVS_SAMPLE_ATTR_ACTIONS:
306                         acts_list = a;
307                         break;
308                 }
309         }
310
311         return do_execute_actions(dp, skb, nla_data(acts_list),
312                                                  nla_len(acts_list), true);
313 }
314
315 static int execute_set_action(struct sk_buff *skb,
316                                  const struct nlattr *nested_attr)
317 {
318         int err = 0;
319
320         switch (nla_type(nested_attr)) {
321         case OVS_KEY_ATTR_PRIORITY:
322                 skb->priority = nla_get_u32(nested_attr);
323                 break;
324
325         case OVS_KEY_ATTR_TUN_ID:
326                 OVS_CB(skb)->tun_id = nla_get_be64(nested_attr);
327                 break;
328
329         case OVS_KEY_ATTR_ETHERNET:
330                 err = set_eth_addr(skb, nla_data(nested_attr));
331                 break;
332
333         case OVS_KEY_ATTR_IPV4:
334                 err = set_ipv4(skb, nla_data(nested_attr));
335                 break;
336
337         case OVS_KEY_ATTR_TCP:
338                 err = set_tcp_port(skb, nla_data(nested_attr));
339                 break;
340
341         case OVS_KEY_ATTR_UDP:
342                 err = set_udp_port(skb, nla_data(nested_attr));
343                 break;
344         }
345
346         return err;
347 }
348
349 /* Execute a list of actions against 'skb'. */
350 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
351                         const struct nlattr *attr, int len, bool keep_skb)
352 {
353         /* Every output action needs a separate clone of 'skb', but the common
354          * case is just a single output action, so that doing a clone and
355          * then freeing the original skbuff is wasteful.  So the following code
356          * is slightly obscure just to avoid that. */
357         int prev_port = -1;
358         const struct nlattr *a;
359         int rem;
360
361         for (a = attr, rem = len; rem > 0;
362              a = nla_next(a, &rem)) {
363                 int err = 0;
364
365                 if (prev_port != -1) {
366                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
367                         prev_port = -1;
368                 }
369
370                 switch (nla_type(a)) {
371                 case OVS_ACTION_ATTR_OUTPUT:
372                         prev_port = nla_get_u32(a);
373                         break;
374
375                 case OVS_ACTION_ATTR_USERSPACE:
376                         output_userspace(dp, skb, a);
377                         break;
378
379                 case OVS_ACTION_ATTR_PUSH_VLAN:
380                         err = push_vlan(skb, nla_data(a));
381                         if (unlikely(err)) /* skb already freed. */
382                                 return err;
383                         break;
384
385                 case OVS_ACTION_ATTR_POP_VLAN:
386                         err = pop_vlan(skb);
387                         break;
388
389                 case OVS_ACTION_ATTR_SET:
390                         err = execute_set_action(skb, nla_data(a));
391                         break;
392
393                 case OVS_ACTION_ATTR_SAMPLE:
394                         err = sample(dp, skb, a);
395                         break;
396                 }
397
398                 if (unlikely(err)) {
399                         kfree_skb(skb);
400                         return err;
401                 }
402         }
403
404         if (prev_port != -1) {
405                 if (keep_skb)
406                         skb = skb_clone(skb, GFP_ATOMIC);
407
408                 do_output(dp, skb, prev_port);
409         } else if (!keep_skb)
410                 consume_skb(skb);
411
412         return 0;
413 }
414
415 /* We limit the number of times that we pass into execute_actions()
416  * to avoid blowing out the stack in the event that we have a loop. */
417 #define MAX_LOOPS 5
418
419 struct loop_counter {
420         u8 count;               /* Count. */
421         bool looping;           /* Loop detected? */
422 };
423
424 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
425
426 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
427 {
428         if (net_ratelimit())
429                 pr_warn("%s: flow looped %d times, dropping\n",
430                                 dp_name(dp), MAX_LOOPS);
431         actions->actions_len = 0;
432         return -ELOOP;
433 }
434
435 /* Execute a list of actions against 'skb'. */
436 int execute_actions(struct datapath *dp, struct sk_buff *skb)
437 {
438         struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
439         struct loop_counter *loop;
440         int error;
441
442         /* Check whether we've looped too much. */
443         loop = &__get_cpu_var(loop_counters);
444         if (unlikely(++loop->count > MAX_LOOPS))
445                 loop->looping = true;
446         if (unlikely(loop->looping)) {
447                 error = loop_suppress(dp, acts);
448                 kfree_skb(skb);
449                 goto out_loop;
450         }
451
452         OVS_CB(skb)->tun_id = 0;
453         error = do_execute_actions(dp, skb, acts->actions,
454                                          acts->actions_len, false);
455
456         /* Check whether sub-actions looped too much. */
457         if (unlikely(loop->looping))
458                 error = loop_suppress(dp, acts);
459
460 out_loop:
461         /* Decrement loop counter. */
462         if (!--loop->count)
463                 loop->looping = false;
464
465         return error;
466 }