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