cf6743513f5c49f8335f0c456d822a3291dcdf55
[sliver-openvswitch.git] / lib / odp-execute.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 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 <linux/openvswitch.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "dpif.h"
25 #include "netlink.h"
26 #include "ofpbuf.h"
27 #include "odp-util.h"
28 #include "packets.h"
29 #include "unaligned.h"
30 #include "util.h"
31
32 static void
33 odp_eth_set_addrs(struct ofpbuf *packet,
34                   const struct ovs_key_ethernet *eth_key)
35 {
36     struct eth_header *eh = packet->l2;
37
38     memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
39     memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
40 }
41
42 static void
43 odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
44 {
45     enum odp_key_fitness fitness;
46
47     fitness = odp_tun_key_from_attr(a, tun_key);
48     ovs_assert(fitness != ODP_FIT_ERROR);
49 }
50
51 static void
52 set_arp(struct ofpbuf *packet, const struct ovs_key_arp *arp_key)
53 {
54     struct arp_eth_header *arp = ofpbuf_l3(packet);
55
56     arp->ar_op = arp_key->arp_op;
57     memcpy(arp->ar_sha, arp_key->arp_sha, ETH_ADDR_LEN);
58     put_16aligned_be32(&arp->ar_spa, arp_key->arp_sip);
59     memcpy(arp->ar_tha, arp_key->arp_tha, ETH_ADDR_LEN);
60     put_16aligned_be32(&arp->ar_tpa, arp_key->arp_tip);
61 }
62
63 static void
64 odp_execute_set_action(struct ofpbuf *packet, const struct nlattr *a,
65                        struct pkt_metadata *md)
66 {
67     enum ovs_key_attr type = nl_attr_type(a);
68     const struct ovs_key_ipv4 *ipv4_key;
69     const struct ovs_key_ipv6 *ipv6_key;
70     const struct ovs_key_tcp *tcp_key;
71     const struct ovs_key_udp *udp_key;
72     const struct ovs_key_sctp *sctp_key;
73
74     switch (type) {
75     case OVS_KEY_ATTR_PRIORITY:
76         md->skb_priority = nl_attr_get_u32(a);
77         break;
78
79     case OVS_KEY_ATTR_TUNNEL:
80         odp_set_tunnel_action(a, &md->tunnel);
81         break;
82
83     case OVS_KEY_ATTR_SKB_MARK:
84         md->pkt_mark = nl_attr_get_u32(a);
85         break;
86
87     case OVS_KEY_ATTR_ETHERNET:
88         odp_eth_set_addrs(packet,
89                           nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
90         break;
91
92     case OVS_KEY_ATTR_IPV4:
93         ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
94         packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
95                         ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
96         break;
97
98     case OVS_KEY_ATTR_IPV6:
99         ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
100         packet_set_ipv6(packet, ipv6_key->ipv6_proto, ipv6_key->ipv6_src,
101                         ipv6_key->ipv6_dst, ipv6_key->ipv6_tclass,
102                         ipv6_key->ipv6_label, ipv6_key->ipv6_hlimit);
103         break;
104
105     case OVS_KEY_ATTR_TCP:
106         tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
107         packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
108         break;
109
110     case OVS_KEY_ATTR_UDP:
111         udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
112         packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
113         break;
114
115     case OVS_KEY_ATTR_SCTP:
116         sctp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
117         packet_set_sctp_port(packet, sctp_key->sctp_src, sctp_key->sctp_dst);
118         break;
119
120     case OVS_KEY_ATTR_MPLS:
121          set_mpls_lse(packet, nl_attr_get_be32(a));
122          break;
123
124     case OVS_KEY_ATTR_ARP:
125         set_arp(packet, nl_attr_get_unspec(a, sizeof(struct ovs_key_arp)));
126         break;
127
128     case OVS_KEY_ATTR_DP_HASH:
129         md->dp_hash = nl_attr_get_u32(a);
130         break;
131
132     case OVS_KEY_ATTR_RECIRC_ID:
133         md->recirc_id = nl_attr_get_u32(a);
134         break;
135
136     case OVS_KEY_ATTR_UNSPEC:
137     case OVS_KEY_ATTR_ENCAP:
138     case OVS_KEY_ATTR_ETHERTYPE:
139     case OVS_KEY_ATTR_IN_PORT:
140     case OVS_KEY_ATTR_VLAN:
141     case OVS_KEY_ATTR_ICMP:
142     case OVS_KEY_ATTR_ICMPV6:
143     case OVS_KEY_ATTR_ND:
144     case OVS_KEY_ATTR_TCP_FLAGS:
145     case __OVS_KEY_ATTR_MAX:
146     default:
147         OVS_NOT_REACHED();
148     }
149 }
150
151 static void
152 odp_execute_actions__(void *dp, struct ofpbuf *packet, bool steal,
153                       struct pkt_metadata *,
154                       const struct nlattr *actions, size_t actions_len,
155                       odp_execute_cb dp_execute_action, bool more_actions);
156
157 static void
158 odp_execute_sample(void *dp, struct ofpbuf *packet, bool steal,
159                    struct pkt_metadata *md, const struct nlattr *action,
160                    odp_execute_cb dp_execute_action, bool more_actions)
161 {
162     const struct nlattr *subactions = NULL;
163     const struct nlattr *a;
164     size_t left;
165
166     NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
167         int type = nl_attr_type(a);
168
169         switch ((enum ovs_sample_attr) type) {
170         case OVS_SAMPLE_ATTR_PROBABILITY:
171             if (random_uint32() >= nl_attr_get_u32(a)) {
172                 return;
173             }
174             break;
175
176         case OVS_SAMPLE_ATTR_ACTIONS:
177             subactions = a;
178             break;
179
180         case OVS_SAMPLE_ATTR_UNSPEC:
181         case __OVS_SAMPLE_ATTR_MAX:
182         default:
183             OVS_NOT_REACHED();
184         }
185     }
186
187     odp_execute_actions__(dp, packet, steal, md, nl_attr_get(subactions),
188                           nl_attr_get_size(subactions), dp_execute_action,
189                           more_actions);
190 }
191
192 static void
193 odp_execute_actions__(void *dp, struct ofpbuf *packet, bool steal,
194                       struct pkt_metadata *md,
195                       const struct nlattr *actions, size_t actions_len,
196                       odp_execute_cb dp_execute_action, bool more_actions)
197 {
198     const struct nlattr *a;
199     unsigned int left;
200
201     NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
202         int type = nl_attr_type(a);
203
204         switch ((enum ovs_action_attr) type) {
205             /* These only make sense in the context of a datapath. */
206         case OVS_ACTION_ATTR_OUTPUT:
207         case OVS_ACTION_ATTR_USERSPACE:
208         case OVS_ACTION_ATTR_RECIRC:
209             if (dp_execute_action) {
210                 bool may_steal;
211                 /* Allow 'dp_execute_action' to steal the packet data if we do
212                  * not need it any more. */
213                 may_steal = steal && (!more_actions && left <= NLA_ALIGN(a->nla_len));
214                 dp_execute_action(dp, packet, md, a, may_steal);
215             }
216             break;
217
218         case OVS_ACTION_ATTR_PUSH_VLAN: {
219             const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
220             eth_push_vlan(packet, htons(ETH_TYPE_VLAN), vlan->vlan_tci);
221             break;
222         }
223
224         case OVS_ACTION_ATTR_POP_VLAN:
225             eth_pop_vlan(packet);
226             break;
227
228         case OVS_ACTION_ATTR_PUSH_MPLS: {
229             const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
230             push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse);
231             break;
232          }
233
234         case OVS_ACTION_ATTR_POP_MPLS:
235             pop_mpls(packet, nl_attr_get_be16(a));
236             break;
237
238         case OVS_ACTION_ATTR_SET:
239             odp_execute_set_action(packet, nl_attr_get(a), md);
240             break;
241
242         case OVS_ACTION_ATTR_SAMPLE:
243             odp_execute_sample(dp, packet, steal, md, a, dp_execute_action,
244                                more_actions || left > NLA_ALIGN(a->nla_len));
245             break;
246
247         case OVS_ACTION_ATTR_UNSPEC:
248         case __OVS_ACTION_ATTR_MAX:
249             OVS_NOT_REACHED();
250         }
251     }
252 }
253
254 void
255 odp_execute_actions(void *dp, struct ofpbuf *packet, bool steal,
256                     struct pkt_metadata *md,
257                     const struct nlattr *actions, size_t actions_len,
258                     odp_execute_cb dp_execute_action)
259 {
260     odp_execute_actions__(dp, packet, steal, md, actions, actions_len,
261                           dp_execute_action, false);
262
263     if (!actions_len && steal) {
264         /* Drop action. */
265         ofpbuf_delete(packet);
266     }
267 }