odp-util: Export odp_tun_key_from_attr()
[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 "netlink.h"
25 #include "ofpbuf.h"
26 #include "packets.h"
27 #include "util.h"
28
29 static void
30 odp_eth_set_addrs(struct ofpbuf *packet, const struct ovs_key_ethernet *eth_key)
31 {
32     struct eth_header *eh = packet->l2;
33
34     memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
35     memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
36 }
37
38 static void
39 odp_execute_set_action(struct ofpbuf *packet, const struct nlattr *a)
40 {
41     enum ovs_key_attr type = nl_attr_type(a);
42     const struct ovs_key_ipv4 *ipv4_key;
43     const struct ovs_key_ipv6 *ipv6_key;
44     const struct ovs_key_tcp *tcp_key;
45     const struct ovs_key_udp *udp_key;
46
47     switch (type) {
48     case OVS_KEY_ATTR_PRIORITY:
49     case OVS_KEY_ATTR_TUNNEL:
50     case OVS_KEY_ATTR_SKB_MARK:
51         /* not implemented */
52         break;
53
54     case OVS_KEY_ATTR_ETHERNET:
55         odp_eth_set_addrs(packet,
56                           nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
57         break;
58
59     case OVS_KEY_ATTR_IPV4:
60         ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
61         packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
62                         ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
63         break;
64
65     case OVS_KEY_ATTR_IPV6:
66         ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
67         packet_set_ipv6(packet, ipv6_key->ipv6_proto, ipv6_key->ipv6_src,
68                         ipv6_key->ipv6_dst, ipv6_key->ipv6_tclass,
69                         ipv6_key->ipv6_label, ipv6_key->ipv6_hlimit);
70         break;
71
72     case OVS_KEY_ATTR_TCP:
73         tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
74         packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
75         break;
76
77      case OVS_KEY_ATTR_UDP:
78         udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
79         packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
80         break;
81
82      case OVS_KEY_ATTR_MPLS:
83          set_mpls_lse(packet, nl_attr_get_be32(a));
84          break;
85
86      case OVS_KEY_ATTR_UNSPEC:
87      case OVS_KEY_ATTR_ENCAP:
88      case OVS_KEY_ATTR_ETHERTYPE:
89      case OVS_KEY_ATTR_IN_PORT:
90      case OVS_KEY_ATTR_VLAN:
91      case OVS_KEY_ATTR_ICMP:
92      case OVS_KEY_ATTR_ICMPV6:
93      case OVS_KEY_ATTR_ARP:
94      case OVS_KEY_ATTR_ND:
95      case __OVS_KEY_ATTR_MAX:
96      default:
97         NOT_REACHED();
98     }
99 }
100
101 static void
102 odp_execute_sample(void *dp, struct ofpbuf *packet, struct flow *key,
103                    const struct nlattr *action,
104                    void (*output)(void *dp, struct ofpbuf *packet,
105                                   uint32_t out_port),
106                    void (*userspace)(void *dp, struct ofpbuf *packet,
107                                      const struct flow *key,
108                                      const struct nlattr *a))
109 {
110     const struct nlattr *subactions = NULL;
111     const struct nlattr *a;
112     size_t left;
113
114     NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
115         int type = nl_attr_type(a);
116
117         switch ((enum ovs_sample_attr) type) {
118         case OVS_SAMPLE_ATTR_PROBABILITY:
119             if (random_uint32() >= nl_attr_get_u32(a)) {
120                 return;
121             }
122             break;
123
124         case OVS_SAMPLE_ATTR_ACTIONS:
125             subactions = a;
126             break;
127
128         case OVS_SAMPLE_ATTR_UNSPEC:
129         case __OVS_SAMPLE_ATTR_MAX:
130         default:
131             NOT_REACHED();
132         }
133     }
134
135     odp_execute_actions(dp, packet, key, nl_attr_get(subactions),
136                         nl_attr_get_size(subactions), output, userspace);
137 }
138
139 void
140 odp_execute_actions(void *dp, struct ofpbuf *packet, struct flow *key,
141                     const struct nlattr *actions, size_t actions_len,
142                     void (*output)(void *dp, struct ofpbuf *packet,
143                                    uint32_t out_port),
144                     void (*userspace)(void *dp, struct ofpbuf *packet,
145                                       const struct flow *key,
146                                       const struct nlattr *a))
147 {
148     const struct nlattr *a;
149     unsigned int left;
150
151     NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
152         int type = nl_attr_type(a);
153
154         switch ((enum ovs_action_attr) type) {
155         case OVS_ACTION_ATTR_OUTPUT:
156             output(dp, packet, nl_attr_get_u32(a));
157             break;
158
159         case OVS_ACTION_ATTR_USERSPACE: {
160             const struct nlattr *userdata;
161
162             userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
163             userspace(dp, packet, key, userdata);
164             break;
165         }
166
167         case OVS_ACTION_ATTR_PUSH_VLAN: {
168             const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
169             eth_push_vlan(packet, vlan->vlan_tci);
170             break;
171         }
172
173         case OVS_ACTION_ATTR_POP_VLAN:
174             eth_pop_vlan(packet);
175             break;
176
177         case OVS_ACTION_ATTR_PUSH_MPLS: {
178             const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
179             push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse);
180             break;
181          }
182
183         case OVS_ACTION_ATTR_POP_MPLS:
184             pop_mpls(packet, nl_attr_get_be16(a));
185             break;
186
187         case OVS_ACTION_ATTR_SET:
188             odp_execute_set_action(packet, nl_attr_get(a));
189             break;
190
191         case OVS_ACTION_ATTR_SAMPLE:
192             odp_execute_sample(dp, packet, key, a, output, userspace);
193             break;
194
195         case OVS_ACTION_ATTR_UNSPEC:
196         case __OVS_ACTION_ATTR_MAX:
197             NOT_REACHED();
198         }
199     }
200 }