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