openvswitch: Userspace tunneling.
[cascardo/ovs.git] / lib / odp-execute.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  * Copyright (c) 2013 Simon Horman
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <config.h>
19 #include "odp-execute.h"
20 #include <arpa/inet.h>
21 #include <netinet/ip6.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "dpif.h"
26 #include "netlink.h"
27 #include "ofpbuf.h"
28 #include "odp-netlink.h"
29 #include "odp-util.h"
30 #include "packet-dpif.h"
31 #include "packets.h"
32 #include "flow.h"
33 #include "unaligned.h"
34 #include "util.h"
35
36 /* Masked copy of an ethernet address. 'src' is already properly masked. */
37 static void
38 ether_addr_copy_masked(uint8_t *dst, const uint8_t *src,
39                        const uint8_t *mask)
40 {
41     int i;
42
43     for (i = 0; i < ETH_ADDR_LEN; i++) {
44         dst[i] = src[i] | (dst[i] & ~mask[i]);
45     }
46 }
47
48 static void
49 odp_eth_set_addrs(struct ofpbuf *packet, const struct ovs_key_ethernet *key,
50                   const struct ovs_key_ethernet *mask)
51 {
52     struct eth_header *eh = ofpbuf_l2(packet);
53
54     if (eh) {
55         if (!mask) {
56             memcpy(eh->eth_src, key->eth_src, sizeof eh->eth_src);
57             memcpy(eh->eth_dst, key->eth_dst, sizeof eh->eth_dst);
58         } else {
59             ether_addr_copy_masked(eh->eth_src, key->eth_src, mask->eth_src);
60             ether_addr_copy_masked(eh->eth_dst, key->eth_dst, mask->eth_dst);
61         }
62     }
63 }
64
65 static void
66 odp_set_ipv4(struct ofpbuf *packet, const struct ovs_key_ipv4 *key,
67              const struct ovs_key_ipv4 *mask)
68 {
69     struct ip_header *nh = ofpbuf_l3(packet);
70
71     packet_set_ipv4(
72         packet,
73         key->ipv4_src | (get_16aligned_be32(&nh->ip_src) & ~mask->ipv4_src),
74         key->ipv4_dst | (get_16aligned_be32(&nh->ip_dst) & ~mask->ipv4_dst),
75         key->ipv4_tos | (nh->ip_tos & ~mask->ipv4_tos),
76         key->ipv4_ttl | (nh->ip_ttl & ~mask->ipv4_ttl));
77 }
78
79 static const ovs_be32 *
80 mask_ipv6_addr(const ovs_16aligned_be32 *old, const ovs_be32 *addr,
81                const ovs_be32 *mask, ovs_be32 *masked)
82 {
83     for (int i = 0; i < 4; i++) {
84         masked[i] = addr[i] | (get_16aligned_be32(&old[i]) & ~mask[i]);
85     }
86
87     return masked;
88 }
89
90 static void
91 odp_set_ipv6(struct ofpbuf *packet, const struct ovs_key_ipv6 *key,
92              const struct ovs_key_ipv6 *mask)
93 {
94     struct ovs_16aligned_ip6_hdr *nh = ofpbuf_l3(packet);
95     ovs_be32 sbuf[4], dbuf[4];
96     uint8_t old_tc = ntohl(get_16aligned_be32(&nh->ip6_flow)) >> 20;
97     ovs_be32 old_fl = get_16aligned_be32(&nh->ip6_flow) & htonl(0xfffff);
98
99     packet_set_ipv6(
100         packet,
101         key->ipv6_proto,
102         mask_ipv6_addr(nh->ip6_src.be32, key->ipv6_src, mask->ipv6_src, sbuf),
103         mask_ipv6_addr(nh->ip6_dst.be32, key->ipv6_dst, mask->ipv6_dst, dbuf),
104         key->ipv6_tclass | (old_tc & ~mask->ipv6_tclass),
105         key->ipv6_label | (old_fl & ~mask->ipv6_label),
106         key->ipv6_hlimit | (nh->ip6_hlim & ~mask->ipv6_hlimit));
107 }
108
109 static void
110 odp_set_tcp(struct ofpbuf *packet, const struct ovs_key_tcp *key,
111              const struct ovs_key_tcp *mask)
112 {
113     struct tcp_header *th = ofpbuf_l4(packet);
114
115     if (OVS_LIKELY(th && ofpbuf_get_tcp_payload(packet))) {
116         packet_set_tcp_port(packet,
117                             key->tcp_src | (th->tcp_src & ~mask->tcp_src),
118                             key->tcp_dst | (th->tcp_dst & ~mask->tcp_dst));
119     }
120 }
121
122 static void
123 odp_set_udp(struct ofpbuf *packet, const struct ovs_key_udp *key,
124              const struct ovs_key_udp *mask)
125 {
126     struct udp_header *uh = ofpbuf_l4(packet);
127
128     if (OVS_LIKELY(uh && ofpbuf_get_udp_payload(packet))) {
129         packet_set_udp_port(packet,
130                             key->udp_src | (uh->udp_src & ~mask->udp_src),
131                             key->udp_dst | (uh->udp_dst & ~mask->udp_dst));
132     }
133 }
134
135 static void
136 odp_set_sctp(struct ofpbuf *packet, const struct ovs_key_sctp *key,
137              const struct ovs_key_sctp *mask)
138 {
139     struct sctp_header *sh = ofpbuf_l4(packet);
140
141     if (OVS_LIKELY(sh && ofpbuf_get_sctp_payload(packet))) {
142         packet_set_sctp_port(packet,
143                              key->sctp_src | (sh->sctp_src & ~mask->sctp_src),
144                              key->sctp_dst | (sh->sctp_dst & ~mask->sctp_dst));
145     }
146 }
147
148 static void
149 odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
150 {
151     enum odp_key_fitness fitness;
152
153     fitness = odp_tun_key_from_attr(a, tun_key);
154     ovs_assert(fitness != ODP_FIT_ERROR);
155 }
156
157 static void
158 set_arp(struct ofpbuf *packet, const struct ovs_key_arp *key,
159         const struct ovs_key_arp *mask)
160 {
161     struct arp_eth_header *arp = ofpbuf_l3(packet);
162
163     if (!mask) {
164         arp->ar_op = key->arp_op;
165         memcpy(arp->ar_sha, key->arp_sha, ETH_ADDR_LEN);
166         put_16aligned_be32(&arp->ar_spa, key->arp_sip);
167         memcpy(arp->ar_tha, key->arp_tha, ETH_ADDR_LEN);
168         put_16aligned_be32(&arp->ar_tpa, key->arp_tip);
169     } else {
170         ovs_be32 ar_spa = get_16aligned_be32(&arp->ar_spa);
171         ovs_be32 ar_tpa = get_16aligned_be32(&arp->ar_tpa);
172
173         arp->ar_op = key->arp_op | (arp->ar_op & ~mask->arp_op);
174         ether_addr_copy_masked(arp->ar_sha, key->arp_sha, mask->arp_sha);
175         put_16aligned_be32(&arp->ar_spa,
176                            key->arp_sip | (ar_spa & ~mask->arp_sip));
177         ether_addr_copy_masked(arp->ar_tha, key->arp_tha, mask->arp_tha);
178         put_16aligned_be32(&arp->ar_tpa,
179                            key->arp_tip | (ar_tpa & ~mask->arp_tip));
180     }
181 }
182
183 static void
184 odp_execute_set_action(struct dpif_packet *packet, const struct nlattr *a)
185 {
186     enum ovs_key_attr type = nl_attr_type(a);
187     const struct ovs_key_ipv4 *ipv4_key;
188     const struct ovs_key_ipv6 *ipv6_key;
189     struct pkt_metadata *md = &packet->md;
190
191     switch (type) {
192     case OVS_KEY_ATTR_PRIORITY:
193         md->skb_priority = nl_attr_get_u32(a);
194         break;
195
196     case OVS_KEY_ATTR_TUNNEL:
197         odp_set_tunnel_action(a, &md->tunnel);
198         break;
199
200     case OVS_KEY_ATTR_SKB_MARK:
201         md->pkt_mark = nl_attr_get_u32(a);
202         break;
203
204     case OVS_KEY_ATTR_ETHERNET:
205         odp_eth_set_addrs(&packet->ofpbuf, nl_attr_get(a), NULL);
206         break;
207
208     case OVS_KEY_ATTR_IPV4:
209         ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
210         packet_set_ipv4(&packet->ofpbuf, ipv4_key->ipv4_src,
211                         ipv4_key->ipv4_dst, ipv4_key->ipv4_tos,
212                         ipv4_key->ipv4_ttl);
213         break;
214
215     case OVS_KEY_ATTR_IPV6:
216         ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
217         packet_set_ipv6(&packet->ofpbuf, ipv6_key->ipv6_proto,
218                         ipv6_key->ipv6_src, ipv6_key->ipv6_dst,
219                         ipv6_key->ipv6_tclass, ipv6_key->ipv6_label,
220                         ipv6_key->ipv6_hlimit);
221         break;
222
223     case OVS_KEY_ATTR_TCP:
224         if (OVS_LIKELY(ofpbuf_get_tcp_payload(&packet->ofpbuf))) {
225             const struct ovs_key_tcp *tcp_key
226                 = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
227
228             packet_set_tcp_port(&packet->ofpbuf, tcp_key->tcp_src,
229                                 tcp_key->tcp_dst);
230         }
231         break;
232
233     case OVS_KEY_ATTR_UDP:
234         if (OVS_LIKELY(ofpbuf_get_udp_payload(&packet->ofpbuf))) {
235             const struct ovs_key_udp *udp_key
236                 = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
237
238             packet_set_udp_port(&packet->ofpbuf, udp_key->udp_src,
239                                 udp_key->udp_dst);
240         }
241         break;
242
243     case OVS_KEY_ATTR_SCTP:
244         if (OVS_LIKELY(ofpbuf_get_sctp_payload(&packet->ofpbuf))) {
245             const struct ovs_key_sctp *sctp_key
246                 = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
247
248             packet_set_sctp_port(&packet->ofpbuf, sctp_key->sctp_src,
249                                  sctp_key->sctp_dst);
250         }
251         break;
252
253     case OVS_KEY_ATTR_MPLS:
254         set_mpls_lse(&packet->ofpbuf, nl_attr_get_be32(a));
255         break;
256
257     case OVS_KEY_ATTR_ARP:
258         set_arp(&packet->ofpbuf, nl_attr_get(a), NULL);
259         break;
260
261     case OVS_KEY_ATTR_DP_HASH:
262         md->dp_hash = nl_attr_get_u32(a);
263         dpif_packet_set_dp_hash(packet, md->dp_hash);
264         break;
265
266     case OVS_KEY_ATTR_RECIRC_ID:
267         md->recirc_id = nl_attr_get_u32(a);
268         break;
269
270     case OVS_KEY_ATTR_UNSPEC:
271     case OVS_KEY_ATTR_ENCAP:
272     case OVS_KEY_ATTR_ETHERTYPE:
273     case OVS_KEY_ATTR_IN_PORT:
274     case OVS_KEY_ATTR_VLAN:
275     case OVS_KEY_ATTR_ICMP:
276     case OVS_KEY_ATTR_ICMPV6:
277     case OVS_KEY_ATTR_ND:
278     case OVS_KEY_ATTR_TCP_FLAGS:
279     case __OVS_KEY_ATTR_MAX:
280     default:
281         OVS_NOT_REACHED();
282     }
283 }
284
285 #define get_mask(a, type) ((const type *)(const void *)(a + 1) + 1)
286
287 static void
288 odp_execute_masked_set_action(struct dpif_packet *packet,
289                               const struct nlattr *a)
290 {
291     struct pkt_metadata *md = &packet->md;
292     enum ovs_key_attr type = nl_attr_type(a);
293     struct mpls_hdr *mh;
294
295     switch (type) {
296     case OVS_KEY_ATTR_PRIORITY:
297         md->skb_priority = nl_attr_get_u32(a)
298             | (md->skb_priority & ~*get_mask(a, uint32_t));
299         break;
300
301     case OVS_KEY_ATTR_SKB_MARK:
302         md->pkt_mark = nl_attr_get_u32(a)
303             | (md->pkt_mark & ~*get_mask(a, uint32_t));
304         break;
305
306     case OVS_KEY_ATTR_ETHERNET:
307         odp_eth_set_addrs(&packet->ofpbuf, nl_attr_get(a),
308                           get_mask(a, struct ovs_key_ethernet));
309         break;
310
311     case OVS_KEY_ATTR_IPV4:
312         odp_set_ipv4(&packet->ofpbuf, nl_attr_get(a),
313                      get_mask(a, struct ovs_key_ipv4));
314         break;
315
316     case OVS_KEY_ATTR_IPV6:
317         odp_set_ipv6(&packet->ofpbuf, nl_attr_get(a),
318                      get_mask(a, struct ovs_key_ipv6));
319         break;
320
321     case OVS_KEY_ATTR_TCP:
322         odp_set_tcp(&packet->ofpbuf, nl_attr_get(a),
323                     get_mask(a, struct ovs_key_tcp));
324         break;
325
326     case OVS_KEY_ATTR_UDP:
327         odp_set_udp(&packet->ofpbuf, nl_attr_get(a),
328                     get_mask(a, struct ovs_key_udp));
329         break;
330
331     case OVS_KEY_ATTR_SCTP:
332         odp_set_sctp(&packet->ofpbuf, nl_attr_get(a),
333                      get_mask(a, struct ovs_key_sctp));
334         break;
335
336     case OVS_KEY_ATTR_MPLS:
337         mh = ofpbuf_l2_5(&packet->ofpbuf);
338         if (mh) {
339             put_16aligned_be32(&mh->mpls_lse, nl_attr_get_be32(a)
340                                | (get_16aligned_be32(&mh->mpls_lse)
341                                   & ~*get_mask(a, ovs_be32)));
342         }
343         break;
344
345     case OVS_KEY_ATTR_ARP:
346         set_arp(&packet->ofpbuf, nl_attr_get(a),
347                 get_mask(a, struct ovs_key_arp));
348         break;
349
350     case OVS_KEY_ATTR_DP_HASH:
351         md->dp_hash = nl_attr_get_u32(a)
352             | (dpif_packet_get_dp_hash(packet) & ~*get_mask(a, uint32_t));
353         dpif_packet_set_dp_hash(packet, md->dp_hash);
354         break;
355
356     case OVS_KEY_ATTR_RECIRC_ID:
357         md->recirc_id = nl_attr_get_u32(a)
358             | (md->recirc_id & ~*get_mask(a, uint32_t));
359         break;
360
361     case OVS_KEY_ATTR_TUNNEL:    /* Masked data not supported for tunnel. */
362     case OVS_KEY_ATTR_UNSPEC:
363     case OVS_KEY_ATTR_ENCAP:
364     case OVS_KEY_ATTR_ETHERTYPE:
365     case OVS_KEY_ATTR_IN_PORT:
366     case OVS_KEY_ATTR_VLAN:
367     case OVS_KEY_ATTR_ICMP:
368     case OVS_KEY_ATTR_ICMPV6:
369     case OVS_KEY_ATTR_ND:
370     case OVS_KEY_ATTR_TCP_FLAGS:
371     case __OVS_KEY_ATTR_MAX:
372     default:
373         OVS_NOT_REACHED();
374     }
375 }
376
377 static void
378 odp_execute_sample(void *dp, struct dpif_packet *packet, bool steal,
379                    const struct nlattr *action,
380                    odp_execute_cb dp_execute_action)
381 {
382     const struct nlattr *subactions = NULL;
383     const struct nlattr *a;
384     size_t left;
385
386     NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
387         int type = nl_attr_type(a);
388
389         switch ((enum ovs_sample_attr) type) {
390         case OVS_SAMPLE_ATTR_PROBABILITY:
391             if (random_uint32() >= nl_attr_get_u32(a)) {
392                 if (steal) {
393                     dpif_packet_delete(packet);
394                 }
395                 return;
396             }
397             break;
398
399         case OVS_SAMPLE_ATTR_ACTIONS:
400             subactions = a;
401             break;
402
403         case OVS_SAMPLE_ATTR_UNSPEC:
404         case __OVS_SAMPLE_ATTR_MAX:
405         default:
406             OVS_NOT_REACHED();
407         }
408     }
409
410     odp_execute_actions(dp, &packet, 1, steal, nl_attr_get(subactions),
411                         nl_attr_get_size(subactions), dp_execute_action);
412 }
413
414 void
415 odp_execute_actions(void *dp, struct dpif_packet **packets, int cnt, bool steal,
416                     const struct nlattr *actions, size_t actions_len,
417                     odp_execute_cb dp_execute_action)
418 {
419     const struct nlattr *a;
420     unsigned int left;
421     int i;
422
423     NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
424         int type = nl_attr_type(a);
425         bool last_action = (left <= NLA_ALIGN(a->nla_len));
426
427         switch ((enum ovs_action_attr) type) {
428             /* These only make sense in the context of a datapath. */
429         case OVS_ACTION_ATTR_OUTPUT:
430         case OVS_ACTION_ATTR_TUNNEL_PUSH:
431         case OVS_ACTION_ATTR_TUNNEL_POP:
432         case OVS_ACTION_ATTR_USERSPACE:
433         case OVS_ACTION_ATTR_RECIRC:
434             if (dp_execute_action) {
435                 /* Allow 'dp_execute_action' to steal the packet data if we do
436                  * not need it any more. */
437                 bool may_steal = steal && last_action;
438
439                 dp_execute_action(dp, packets, cnt, a, may_steal);
440
441                 if (last_action) {
442                     /* We do not need to free the packets. dp_execute_actions()
443                      * has stolen them */
444                     return;
445                 }
446             }
447             break;
448
449         case OVS_ACTION_ATTR_HASH: {
450             const struct ovs_action_hash *hash_act = nl_attr_get(a);
451
452             /* Calculate a hash value directly.  This might not match the
453              * value computed by the datapath, but it is much less expensive,
454              * and the current use case (bonding) does not require a strict
455              * match to work properly. */
456             if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
457                 struct flow flow;
458                 uint32_t hash;
459
460                 for (i = 0; i < cnt; i++) {
461                     flow_extract(&packets[i]->ofpbuf, &packets[i]->md, &flow);
462                     hash = flow_hash_5tuple(&flow, hash_act->hash_basis);
463
464                     /* We also store the hash value with each packet */
465                     dpif_packet_set_dp_hash(packets[i], hash ? hash : 1);
466                 }
467             } else {
468                 /* Assert on unknown hash algorithm.  */
469                 OVS_NOT_REACHED();
470             }
471             break;
472         }
473
474         case OVS_ACTION_ATTR_PUSH_VLAN: {
475             const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
476
477             for (i = 0; i < cnt; i++) {
478                 struct ofpbuf *buf = &packets[i]->ofpbuf;
479
480                 eth_push_vlan(buf, htons(ETH_TYPE_VLAN), vlan->vlan_tci);
481             }
482             break;
483         }
484
485         case OVS_ACTION_ATTR_POP_VLAN:
486             for (i = 0; i < cnt; i++) {
487                 struct ofpbuf *buf = &packets[i]->ofpbuf;
488
489                 eth_pop_vlan(buf);
490             }
491             break;
492
493         case OVS_ACTION_ATTR_PUSH_MPLS: {
494             const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
495
496             for (i = 0; i < cnt; i++) {
497                 struct ofpbuf *buf = &packets[i]->ofpbuf;
498
499                 push_mpls(buf, mpls->mpls_ethertype, mpls->mpls_lse);
500             }
501             break;
502          }
503
504         case OVS_ACTION_ATTR_POP_MPLS:
505             for (i = 0; i < cnt; i++) {
506                 struct ofpbuf *buf = &packets[i]->ofpbuf;
507
508                 pop_mpls(buf, nl_attr_get_be16(a));
509             }
510             break;
511
512         case OVS_ACTION_ATTR_SET:
513             for (i = 0; i < cnt; i++) {
514                 odp_execute_set_action(packets[i], nl_attr_get(a));
515             }
516             break;
517
518         case OVS_ACTION_ATTR_SET_MASKED:
519             for (i = 0; i < cnt; i++) {
520                 odp_execute_masked_set_action(packets[i], nl_attr_get(a));
521             }
522             break;
523
524         case OVS_ACTION_ATTR_SAMPLE:
525             for (i = 0; i < cnt; i++) {
526                 odp_execute_sample(dp, packets[i], steal && last_action, a,
527                                    dp_execute_action);
528             }
529
530             if (last_action) {
531                 /* We do not need to free the packets. odp_execute_sample() has
532                  * stolen them*/
533                 return;
534             }
535             break;
536
537         case OVS_ACTION_ATTR_UNSPEC:
538         case __OVS_ACTION_ATTR_MAX:
539             OVS_NOT_REACHED();
540         }
541     }
542
543     if (steal) {
544         for (i = 0; i < cnt; i++) {
545             dpif_packet_delete(packets[i]);
546         }
547     }
548 }