09f8b8384d7c75b299daf37ae4c2a7da807a3c79
[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 "packets.h"
28 #include "timeval.h"
29 #include "util.h"
30
31 void
32 format_odp_flow_key(struct ds *ds, const struct odp_flow_key *key)
33 {
34     ds_put_format(ds, "tun_id%#"PRIx64" in_port%d tci(",
35                   ntohll(key->tun_id), key->in_port);
36     if (key->dl_tci) {
37         ds_put_format(ds, "vlan%"PRIu16",pcp%d",
38                       vlan_tci_to_vid(key->dl_tci),
39                       vlan_tci_to_pcp(key->dl_tci));
40     } else {
41         ds_put_char(ds, '0');
42     }
43     ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" type%04x "
44                   "proto%"PRId8" tos%"PRIu8" ip"IP_FMT"->"IP_FMT" port%d->%d",
45                   ETH_ADDR_ARGS(key->dl_src), ETH_ADDR_ARGS(key->dl_dst),
46                   ntohs(key->dl_type), key->nw_proto, key->nw_tos,
47                   IP_ARGS(&key->nw_src), IP_ARGS(&key->nw_dst),
48                   ntohs(key->tp_src), ntohs(key->tp_dst));
49 }
50
51 int
52 odp_action_len(uint16_t type)
53 {
54     if (type > ODPAT_MAX) {
55         return -1;
56     }
57
58     switch ((enum odp_action_type) type) {
59     case ODPAT_OUTPUT: return 4;
60     case ODPAT_CONTROLLER: return 8;
61     case ODPAT_SET_DL_TCI: return 2;
62     case ODPAT_STRIP_VLAN: return 0;
63     case ODPAT_SET_DL_SRC: return ETH_ADDR_LEN;
64     case ODPAT_SET_DL_DST: return ETH_ADDR_LEN;
65     case ODPAT_SET_NW_SRC: return 4;
66     case ODPAT_SET_NW_DST: return 4;
67     case ODPAT_SET_NW_TOS: return 1;
68     case ODPAT_SET_TP_SRC: return 2;
69     case ODPAT_SET_TP_DST: return 2;
70     case ODPAT_SET_TUNNEL: return 8;
71     case ODPAT_SET_PRIORITY: return 4;
72     case ODPAT_POP_PRIORITY: return 0;
73     case ODPAT_DROP_SPOOFED_ARP: return 0;
74
75     case ODPAT_UNSPEC:
76     case __ODPAT_MAX:
77         return -1;
78     }
79
80     return -1;
81 }
82
83 static void
84 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
85 {
86     size_t len = nl_attr_get_size(a);
87
88     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
89     if (len) {
90         const uint8_t *unspec;
91         unsigned int i;
92
93         unspec = nl_attr_get(a);
94         for (i = 0; i < len; i++) {
95             ds_put_char(ds, i ? ' ': '(');
96             ds_put_format(ds, "%02x", unspec[i]);
97         }
98         ds_put_char(ds, ')');
99     }
100 }
101
102 void
103 format_odp_action(struct ds *ds, const struct nlattr *a)
104 {
105     const uint8_t *eth;
106     ovs_be32 ip;
107
108     if (nl_attr_get_size(a) != odp_action_len(nl_attr_type(a))) {
109         ds_put_format(ds, "bad length %zu, expected %d for: ",
110                       nl_attr_get_size(a), odp_action_len(nl_attr_type(a)));
111         format_generic_odp_action(ds, a);
112         return;
113     }
114
115     switch (nl_attr_type(a)) {
116     case ODPAT_OUTPUT:
117         ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
118         break;
119     case ODPAT_CONTROLLER:
120         ds_put_format(ds, "ctl(%"PRIu64")", nl_attr_get_u64(a));
121         break;
122     case ODPAT_SET_TUNNEL:
123         ds_put_format(ds, "set_tunnel(%#"PRIx64")",
124                       ntohll(nl_attr_get_be64(a)));
125         break;
126     case ODPAT_SET_DL_TCI:
127         ds_put_format(ds, "set_tci(vid=%"PRIu16",pcp=%d)",
128                       vlan_tci_to_vid(nl_attr_get_be16(a)),
129                       vlan_tci_to_pcp(nl_attr_get_be16(a)));
130         break;
131     case ODPAT_STRIP_VLAN:
132         ds_put_format(ds, "strip_vlan");
133         break;
134     case ODPAT_SET_DL_SRC:
135         eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
136         ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
137         break;
138     case ODPAT_SET_DL_DST:
139         eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
140         ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
141         break;
142     case ODPAT_SET_NW_SRC:
143         ip = nl_attr_get_be32(a);
144         ds_put_format(ds, "set_nw_src("IP_FMT")", IP_ARGS(&ip));
145         break;
146     case ODPAT_SET_NW_DST:
147         ip = nl_attr_get_be32(a);
148         ds_put_format(ds, "set_nw_dst("IP_FMT")", IP_ARGS(&ip));
149         break;
150     case ODPAT_SET_NW_TOS:
151         ds_put_format(ds, "set_nw_tos(%"PRIu8")", nl_attr_get_u8(a));
152         break;
153     case ODPAT_SET_TP_SRC:
154         ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
155         break;
156     case ODPAT_SET_TP_DST:
157         ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
158         break;
159     case ODPAT_SET_PRIORITY:
160         ds_put_format(ds, "set_priority(%#"PRIx32")", nl_attr_get_u32(a));
161         break;
162     case ODPAT_POP_PRIORITY:
163         ds_put_cstr(ds, "pop_priority");
164         break;
165     case ODPAT_DROP_SPOOFED_ARP:
166         ds_put_cstr(ds, "drop_spoofed_arp");
167         break;
168     default:
169         format_generic_odp_action(ds, a);
170         break;
171     }
172 }
173
174 void
175 format_odp_actions(struct ds *ds, const struct nlattr *actions,
176                    size_t actions_len)
177 {
178     if (actions_len) {
179         const struct nlattr *a;
180         unsigned int left;
181
182         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
183             if (a != actions) {
184                 ds_put_char(ds, ',');
185             }
186             format_odp_action(ds, a);
187         }
188         if (left) {
189             ds_put_format(ds, " ***%u leftover bytes***", left);
190         }
191     } else {
192         ds_put_cstr(ds, "drop");
193     }
194 }
195
196 void
197 format_odp_flow_stats(struct ds *ds, const struct odp_flow_stats *s)
198 {
199     ds_put_format(ds, "packets:%llu, bytes:%llu, used:",
200                   (unsigned long long int) s->n_packets,
201                   (unsigned long long int) s->n_bytes);
202     if (s->used_sec) {
203         long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
204         ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
205     } else {
206         ds_put_format(ds, "never");
207     }
208 }
209
210 void
211 format_odp_flow(struct ds *ds, const struct odp_flow *f)
212 {
213     format_odp_flow_key(ds, &f->key);
214     ds_put_cstr(ds, ", ");
215     format_odp_flow_stats(ds, &f->stats);
216     ds_put_cstr(ds, ", actions:");
217     format_odp_actions(ds, f->actions, f->actions_len);
218 }
219 \f
220 void
221 odp_flow_key_from_flow(struct odp_flow_key *key, const struct flow *flow)
222 {
223     key->tun_id = flow->tun_id;
224     key->nw_src = flow->nw_src;
225     key->nw_dst = flow->nw_dst;
226     key->in_port = flow->in_port;
227     key->dl_tci = flow->vlan_tci;
228     key->dl_type = flow->dl_type;
229     key->tp_src = flow->tp_src;
230     key->tp_dst = flow->tp_dst;
231     memcpy(key->dl_src, flow->dl_src, ETH_ADDR_LEN);
232     memcpy(key->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
233     key->nw_proto = flow->nw_proto;
234     key->nw_tos = flow->nw_tos;
235 }
236
237 void
238 odp_flow_key_to_flow(const struct odp_flow_key *key, struct flow *flow)
239 {
240     memset(flow->regs, 0, sizeof flow->regs);
241     flow->tun_id = key->tun_id;
242     flow->nw_src = key->nw_src;
243     flow->nw_dst = key->nw_dst;
244     flow->in_port = key->in_port;
245     flow->vlan_tci = key->dl_tci;
246     flow->dl_type = key->dl_type;
247     flow->tp_src = key->tp_src;
248     flow->tp_dst = key->tp_dst;
249     memcpy(flow->dl_src, key->dl_src, ETH_ADDR_LEN);
250     memcpy(flow->dl_dst, key->dl_dst, ETH_ADDR_LEN);
251     flow->nw_proto = key->nw_proto;
252     flow->nw_tos = key->nw_tos;
253 }