1f6d93e9a38c144e1a331105eeaeec44bed600af
[sliver-openvswitch.git] / lib / odp-util.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "odp-util.h"
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "byte-order.h"
23 #include "coverage.h"
24 #include "dynamic-string.h"
25 #include "flow.h"
26 #include "netlink.h"
27 #include "openvswitch/tunnel.h"
28 #include "packets.h"
29 #include "timeval.h"
30 #include "util.h"
31
32 void
33 format_odp_flow_key(struct ds *ds, const struct odp_flow_key *key)
34 {
35     ds_put_format(ds, "tun_id%#"PRIx64" in_port%d tci(",
36                   ntohll(key->tun_id), key->in_port);
37     if (key->dl_tci) {
38         ds_put_format(ds, "vlan%"PRIu16",pcp%d",
39                       vlan_tci_to_vid(key->dl_tci),
40                       vlan_tci_to_pcp(key->dl_tci));
41     } else {
42         ds_put_char(ds, '0');
43     }
44     ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" type%04x "
45                   "proto%"PRId8" tos%"PRIu8" ip"IP_FMT"->"IP_FMT" port%d->%d",
46                   ETH_ADDR_ARGS(key->dl_src), ETH_ADDR_ARGS(key->dl_dst),
47                   ntohs(key->dl_type), key->nw_proto, key->nw_tos,
48                   IP_ARGS(&key->nw_src), IP_ARGS(&key->nw_dst),
49                   ntohs(key->tp_src), ntohs(key->tp_dst));
50 }
51
52 int
53 odp_action_len(uint16_t type)
54 {
55     if (type > ODPAT_MAX) {
56         return -1;
57     }
58
59     switch ((enum odp_action_type) type) {
60     case ODPAT_OUTPUT: return 4;
61     case ODPAT_CONTROLLER: return 8;
62     case ODPAT_SET_DL_TCI: return 2;
63     case ODPAT_STRIP_VLAN: return 0;
64     case ODPAT_SET_DL_SRC: return ETH_ADDR_LEN;
65     case ODPAT_SET_DL_DST: return ETH_ADDR_LEN;
66     case ODPAT_SET_NW_SRC: return 4;
67     case ODPAT_SET_NW_DST: return 4;
68     case ODPAT_SET_NW_TOS: return 1;
69     case ODPAT_SET_TP_SRC: return 2;
70     case ODPAT_SET_TP_DST: return 2;
71     case ODPAT_SET_TUNNEL: return 8;
72     case ODPAT_SET_PRIORITY: return 4;
73     case ODPAT_POP_PRIORITY: return 0;
74     case ODPAT_DROP_SPOOFED_ARP: return 0;
75
76     case ODPAT_UNSPEC:
77     case __ODPAT_MAX:
78         return -1;
79     }
80
81     return -1;
82 }
83
84 static void
85 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
86 {
87     size_t len = nl_attr_get_size(a);
88
89     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
90     if (len) {
91         const uint8_t *unspec;
92         unsigned int i;
93
94         unspec = nl_attr_get(a);
95         for (i = 0; i < len; i++) {
96             ds_put_char(ds, i ? ' ': '(');
97             ds_put_format(ds, "%02x", unspec[i]);
98         }
99         ds_put_char(ds, ')');
100     }
101 }
102
103 void
104 format_odp_action(struct ds *ds, const struct nlattr *a)
105 {
106     const uint8_t *eth;
107     ovs_be32 ip;
108
109     if (nl_attr_get_size(a) != odp_action_len(nl_attr_type(a))) {
110         ds_put_format(ds, "bad length %zu, expected %d for: ",
111                       nl_attr_get_size(a), odp_action_len(nl_attr_type(a)));
112         format_generic_odp_action(ds, a);
113         return;
114     }
115
116     switch (nl_attr_type(a)) {
117     case ODPAT_OUTPUT:
118         ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
119         break;
120     case ODPAT_CONTROLLER:
121         ds_put_format(ds, "ctl(%"PRIu64")", nl_attr_get_u64(a));
122         break;
123     case ODPAT_SET_TUNNEL:
124         ds_put_format(ds, "set_tunnel(%#"PRIx64")",
125                       ntohll(nl_attr_get_be64(a)));
126         break;
127     case ODPAT_SET_DL_TCI:
128         ds_put_format(ds, "set_tci(vid=%"PRIu16",pcp=%d)",
129                       vlan_tci_to_vid(nl_attr_get_be16(a)),
130                       vlan_tci_to_pcp(nl_attr_get_be16(a)));
131         break;
132     case ODPAT_STRIP_VLAN:
133         ds_put_format(ds, "strip_vlan");
134         break;
135     case ODPAT_SET_DL_SRC:
136         eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
137         ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
138         break;
139     case ODPAT_SET_DL_DST:
140         eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
141         ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
142         break;
143     case ODPAT_SET_NW_SRC:
144         ip = nl_attr_get_be32(a);
145         ds_put_format(ds, "set_nw_src("IP_FMT")", IP_ARGS(&ip));
146         break;
147     case ODPAT_SET_NW_DST:
148         ip = nl_attr_get_be32(a);
149         ds_put_format(ds, "set_nw_dst("IP_FMT")", IP_ARGS(&ip));
150         break;
151     case ODPAT_SET_NW_TOS:
152         ds_put_format(ds, "set_nw_tos(%"PRIu8")", nl_attr_get_u8(a));
153         break;
154     case ODPAT_SET_TP_SRC:
155         ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
156         break;
157     case ODPAT_SET_TP_DST:
158         ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
159         break;
160     case ODPAT_SET_PRIORITY:
161         ds_put_format(ds, "set_priority(%#"PRIx32")", nl_attr_get_u32(a));
162         break;
163     case ODPAT_POP_PRIORITY:
164         ds_put_cstr(ds, "pop_priority");
165         break;
166     case ODPAT_DROP_SPOOFED_ARP:
167         ds_put_cstr(ds, "drop_spoofed_arp");
168         break;
169     default:
170         format_generic_odp_action(ds, a);
171         break;
172     }
173 }
174
175 void
176 format_odp_actions(struct ds *ds, const struct nlattr *actions,
177                    size_t actions_len)
178 {
179     if (actions_len) {
180         const struct nlattr *a;
181         unsigned int left;
182
183         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
184             if (a != actions) {
185                 ds_put_char(ds, ',');
186             }
187             format_odp_action(ds, a);
188         }
189         if (left) {
190             ds_put_format(ds, " ***%u leftover bytes***", left);
191         }
192     } else {
193         ds_put_cstr(ds, "drop");
194     }
195 }
196
197 void
198 format_odp_flow_stats(struct ds *ds, const struct odp_flow_stats *s)
199 {
200     ds_put_format(ds, "packets:%llu, bytes:%llu, used:",
201                   (unsigned long long int) s->n_packets,
202                   (unsigned long long int) s->n_bytes);
203     if (s->used_sec) {
204         long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
205         ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
206     } else {
207         ds_put_format(ds, "never");
208     }
209 }
210
211 void
212 format_odp_flow(struct ds *ds, const struct odp_flow *f)
213 {
214     format_odp_flow_key(ds, &f->key);
215     ds_put_cstr(ds, ", ");
216     format_odp_flow_stats(ds, &f->stats);
217     ds_put_cstr(ds, ", actions:");
218     format_odp_actions(ds, f->actions, f->actions_len);
219 }
220
221 void
222 format_odp_port_type(struct ds *ds, const struct odp_port *p)
223 {
224     if (!strcmp(p->type, "gre") 
225             || !strcmp(p->type, "capwap")) {
226         const struct tnl_port_config *config;
227
228         config = (struct tnl_port_config *)p->config;
229
230         ds_put_format(ds, " (%s: remote_ip="IP_FMT, 
231                 p->type, IP_ARGS(&config->daddr));
232
233         if (config->saddr) {
234             ds_put_format(ds, ", local_ip="IP_FMT, IP_ARGS(&config->saddr));
235         }
236
237         if (config->in_key) {
238             ds_put_format(ds, ", in_key=%#"PRIx64, ntohll(config->in_key));
239         }
240
241         ds_put_cstr(ds, ")");
242     } else if (!strcmp(p->type, "patch")) {
243         ds_put_format(ds, " (%s: peer=%s)", p->type, (char *)p->config);
244     } else if (strcmp(p->type, "system")) {
245         ds_put_format(ds, " (%s)", p->type);
246     }
247 }
248 \f
249 void
250 odp_flow_key_from_flow(struct odp_flow_key *key, const struct flow *flow)
251 {
252     key->tun_id = flow->tun_id;
253     key->nw_src = flow->nw_src;
254     key->nw_dst = flow->nw_dst;
255     key->in_port = flow->in_port;
256     key->dl_tci = flow->vlan_tci;
257     key->dl_type = flow->dl_type;
258     key->tp_src = flow->tp_src;
259     key->tp_dst = flow->tp_dst;
260     memcpy(key->dl_src, flow->dl_src, ETH_ADDR_LEN);
261     memcpy(key->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
262     key->nw_proto = flow->nw_proto;
263     key->nw_tos = flow->nw_tos;
264 }
265
266 void
267 odp_flow_key_to_flow(const struct odp_flow_key *key, struct flow *flow)
268 {
269     memset(flow->regs, 0, sizeof flow->regs);
270     flow->tun_id = key->tun_id;
271     flow->nw_src = key->nw_src;
272     flow->nw_dst = key->nw_dst;
273     flow->in_port = key->in_port;
274     flow->vlan_tci = key->dl_tci;
275     flow->dl_type = key->dl_type;
276     flow->tp_src = key->tp_src;
277     flow->tp_dst = key->tp_dst;
278     memcpy(flow->dl_src, key->dl_src, ETH_ADDR_LEN);
279     memcpy(flow->dl_dst, key->dl_dst, ETH_ADDR_LEN);
280     flow->nw_proto = key->nw_proto;
281     flow->nw_tos = key->nw_tos;
282 }