Prepare include headers
[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 <stdlib.h>
21 #include <string.h>
22
23 #include "dpif.h"
24 #include "netlink.h"
25 #include "ofpbuf.h"
26 #include "odp-netlink.h"
27 #include "odp-util.h"
28 #include "packet-dpif.h"
29 #include "packets.h"
30 #include "flow.h"
31 #include "unaligned.h"
32 #include "util.h"
33
34 static void
35 odp_eth_set_addrs(struct ofpbuf *packet,
36                   const struct ovs_key_ethernet *eth_key)
37 {
38     struct eth_header *eh = ofpbuf_l2(packet);
39
40     if (eh) {
41         memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
42         memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
43     }
44 }
45
46 static void
47 odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
48 {
49     enum odp_key_fitness fitness;
50
51     fitness = odp_tun_key_from_attr(a, tun_key);
52     ovs_assert(fitness != ODP_FIT_ERROR);
53 }
54
55 static void
56 set_arp(struct ofpbuf *packet, const struct ovs_key_arp *arp_key)
57 {
58     struct arp_eth_header *arp = ofpbuf_l3(packet);
59
60     arp->ar_op = arp_key->arp_op;
61     memcpy(arp->ar_sha, arp_key->arp_sha, ETH_ADDR_LEN);
62     put_16aligned_be32(&arp->ar_spa, arp_key->arp_sip);
63     memcpy(arp->ar_tha, arp_key->arp_tha, ETH_ADDR_LEN);
64     put_16aligned_be32(&arp->ar_tpa, arp_key->arp_tip);
65 }
66
67 static void
68 odp_execute_set_action(struct dpif_packet *packet, const struct nlattr *a,
69                        struct pkt_metadata *md)
70 {
71     enum ovs_key_attr type = nl_attr_type(a);
72     const struct ovs_key_ipv4 *ipv4_key;
73     const struct ovs_key_ipv6 *ipv6_key;
74     const struct ovs_key_tcp *tcp_key;
75     const struct ovs_key_udp *udp_key;
76     const struct ovs_key_sctp *sctp_key;
77
78     switch (type) {
79     case OVS_KEY_ATTR_PRIORITY:
80         md->skb_priority = nl_attr_get_u32(a);
81         break;
82
83     case OVS_KEY_ATTR_TUNNEL:
84         odp_set_tunnel_action(a, &md->tunnel);
85         break;
86
87     case OVS_KEY_ATTR_SKB_MARK:
88         md->pkt_mark = nl_attr_get_u32(a);
89         break;
90
91     case OVS_KEY_ATTR_ETHERNET:
92         odp_eth_set_addrs(&packet->ofpbuf,
93                        nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
94         break;
95
96     case OVS_KEY_ATTR_IPV4:
97         ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
98         packet_set_ipv4(&packet->ofpbuf, ipv4_key->ipv4_src,
99                         ipv4_key->ipv4_dst, ipv4_key->ipv4_tos,
100                         ipv4_key->ipv4_ttl);
101         break;
102
103     case OVS_KEY_ATTR_IPV6:
104         ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
105         packet_set_ipv6(&packet->ofpbuf, ipv6_key->ipv6_proto,
106                         ipv6_key->ipv6_src, ipv6_key->ipv6_dst,
107                         ipv6_key->ipv6_tclass, ipv6_key->ipv6_label,
108                         ipv6_key->ipv6_hlimit);
109         break;
110
111     case OVS_KEY_ATTR_TCP:
112         tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
113         packet_set_tcp_port(&packet->ofpbuf, tcp_key->tcp_src,
114                             tcp_key->tcp_dst);
115         break;
116
117     case OVS_KEY_ATTR_UDP:
118         udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
119         packet_set_udp_port(&packet->ofpbuf, udp_key->udp_src,
120                             udp_key->udp_dst);
121         break;
122
123     case OVS_KEY_ATTR_SCTP:
124         sctp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
125         packet_set_sctp_port(&packet->ofpbuf, sctp_key->sctp_src,
126                              sctp_key->sctp_dst);
127         break;
128
129     case OVS_KEY_ATTR_MPLS:
130          set_mpls_lse(&packet->ofpbuf, nl_attr_get_be32(a));
131          break;
132
133     case OVS_KEY_ATTR_ARP:
134         set_arp(&packet->ofpbuf,
135                 nl_attr_get_unspec(a, sizeof(struct ovs_key_arp)));
136         break;
137
138     case OVS_KEY_ATTR_DP_HASH:
139         packet->dp_hash = md->dp_hash = nl_attr_get_u32(a);
140         break;
141
142     case OVS_KEY_ATTR_RECIRC_ID:
143         md->recirc_id = nl_attr_get_u32(a);
144         break;
145
146     case OVS_KEY_ATTR_UNSPEC:
147     case OVS_KEY_ATTR_ENCAP:
148     case OVS_KEY_ATTR_ETHERTYPE:
149     case OVS_KEY_ATTR_IN_PORT:
150     case OVS_KEY_ATTR_VLAN:
151     case OVS_KEY_ATTR_ICMP:
152     case OVS_KEY_ATTR_ICMPV6:
153     case OVS_KEY_ATTR_ND:
154     case OVS_KEY_ATTR_TCP_FLAGS:
155     case __OVS_KEY_ATTR_MAX:
156     default:
157         OVS_NOT_REACHED();
158     }
159 }
160
161 static void
162 odp_execute_actions__(void *dp, struct dpif_packet **packets, int cnt,
163                       bool steal, struct pkt_metadata *,
164                       const struct nlattr *actions, size_t actions_len,
165                       odp_execute_cb dp_execute_action, bool more_actions);
166
167 static void
168 odp_execute_sample(void *dp, struct dpif_packet *packet, bool steal,
169                    struct pkt_metadata *md, const struct nlattr *action,
170                    odp_execute_cb dp_execute_action, bool more_actions)
171 {
172     const struct nlattr *subactions = NULL;
173     const struct nlattr *a;
174     size_t left;
175
176     NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
177         int type = nl_attr_type(a);
178
179         switch ((enum ovs_sample_attr) type) {
180         case OVS_SAMPLE_ATTR_PROBABILITY:
181             if (random_uint32() >= nl_attr_get_u32(a)) {
182                 return;
183             }
184             break;
185
186         case OVS_SAMPLE_ATTR_ACTIONS:
187             subactions = a;
188             break;
189
190         case OVS_SAMPLE_ATTR_UNSPEC:
191         case __OVS_SAMPLE_ATTR_MAX:
192         default:
193             OVS_NOT_REACHED();
194         }
195     }
196
197     odp_execute_actions__(dp, &packet, 1, steal, md, nl_attr_get(subactions),
198                           nl_attr_get_size(subactions), dp_execute_action,
199                           more_actions);
200 }
201
202 static void
203 odp_execute_actions__(void *dp, struct dpif_packet **packets, int cnt,
204                       bool steal, struct pkt_metadata *md,
205                       const struct nlattr *actions, size_t actions_len,
206                       odp_execute_cb dp_execute_action, bool more_actions)
207 {
208     const struct nlattr *a;
209     unsigned int left;
210
211     int i;
212
213     NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
214         int type = nl_attr_type(a);
215
216         switch ((enum ovs_action_attr) type) {
217             /* These only make sense in the context of a datapath. */
218         case OVS_ACTION_ATTR_OUTPUT:
219         case OVS_ACTION_ATTR_USERSPACE:
220         case OVS_ACTION_ATTR_RECIRC:
221             if (dp_execute_action) {
222                 /* Allow 'dp_execute_action' to steal the packet data if we do
223                  * not need it any more. */
224                 bool may_steal = steal && (!more_actions
225                                            && left <= NLA_ALIGN(a->nla_len)
226                                            && type != OVS_ACTION_ATTR_RECIRC);
227                 dp_execute_action(dp, packets, cnt, md, a, may_steal);
228             }
229             break;
230
231         case OVS_ACTION_ATTR_HASH: {
232             const struct ovs_action_hash *hash_act = nl_attr_get(a);
233
234             /* Calculate a hash value directly.  This might not match the
235              * value computed by the datapath, but it is much less expensive,
236              * and the current use case (bonding) does not require a strict
237              * match to work properly. */
238             if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
239                 struct flow flow;
240                 uint32_t hash;
241
242                 for (i = 0; i < cnt; i++) {
243                     struct ofpbuf *buf = &packets[i]->ofpbuf;
244
245                     flow_extract(buf, md, &flow);
246                     hash = flow_hash_5tuple(&flow, hash_act->hash_basis);
247
248                     /* The hash of the first packet is in shared metadata */
249                     if (i == 0) {
250                         md->dp_hash = hash ? hash : 1;
251                     }
252
253                     /* We also store the hash value with each packet */
254                     packets[i]->dp_hash = hash ? hash : 1;
255                 }
256             } else {
257                 /* Assert on unknown hash algorithm.  */
258                 OVS_NOT_REACHED();
259             }
260             break;
261         }
262
263         case OVS_ACTION_ATTR_PUSH_VLAN: {
264             const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
265
266             for (i = 0; i < cnt; i++) {
267                 struct ofpbuf *buf = &packets[i]->ofpbuf;
268
269                 eth_push_vlan(buf, htons(ETH_TYPE_VLAN), vlan->vlan_tci);
270             }
271             break;
272         }
273
274         case OVS_ACTION_ATTR_POP_VLAN:
275             for (i = 0; i < cnt; i++) {
276                 struct ofpbuf *buf = &packets[i]->ofpbuf;
277
278                 eth_pop_vlan(buf);
279             }
280             break;
281
282         case OVS_ACTION_ATTR_PUSH_MPLS: {
283             const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
284
285             for (i = 0; i < cnt; i++) {
286                 struct ofpbuf *buf = &packets[i]->ofpbuf;
287
288                 push_mpls(buf, mpls->mpls_ethertype, mpls->mpls_lse);
289             }
290             break;
291          }
292
293         case OVS_ACTION_ATTR_POP_MPLS:
294             for (i = 0; i < cnt; i++) {
295                 struct ofpbuf *buf = &packets[i]->ofpbuf;
296
297                 pop_mpls(buf, nl_attr_get_be16(a));
298             }
299             break;
300
301         case OVS_ACTION_ATTR_SET:
302             for (i = 0; i < cnt; i++) {
303                 odp_execute_set_action(packets[i], nl_attr_get(a),
304                                        md);
305             }
306             break;
307
308         case OVS_ACTION_ATTR_SAMPLE:
309             for (i = 0; i < cnt; i++) {
310                 odp_execute_sample(dp, packets[i], steal, md, a,
311                                    dp_execute_action,
312                                    more_actions ||
313                                    left > NLA_ALIGN(a->nla_len));
314             }
315             break;
316
317         case OVS_ACTION_ATTR_UNSPEC:
318         case __OVS_ACTION_ATTR_MAX:
319             OVS_NOT_REACHED();
320         }
321     }
322 }
323
324 void
325 odp_execute_actions(void *dp, struct dpif_packet **packets, int cnt,
326                     bool steal, struct pkt_metadata *md,
327                     const struct nlattr *actions, size_t actions_len,
328                     odp_execute_cb dp_execute_action)
329 {
330     odp_execute_actions__(dp, packets, cnt, steal, md, actions, actions_len,
331                           dp_execute_action, false);
332
333     if (!actions_len && steal) {
334         /* Drop action. */
335         int i;
336
337         for (i = 0; i < cnt; i++) {
338             dpif_packet_delete(packets[i]);
339         }
340     }
341 }