datapath: Replace "struct odp_action" by Netlink attributes.
[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%#"PRIx32" in_port%d tci(",
35                   ntohl(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 4;
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 4;
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     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
87     if (a->nla_len) {
88         const uint8_t *unspec;
89         unsigned int i;
90
91         unspec = nl_attr_get(a);
92         for (i = 0; i < a->nla_len; i++) {
93             ds_put_char(ds, i ? ' ': '(');
94             ds_put_format(ds, "%02x", unspec[i]);
95         }
96         ds_put_char(ds, ')');
97     }
98 }
99
100 void
101 format_odp_action(struct ds *ds, const struct nlattr *a)
102 {
103     const uint8_t *eth;
104     ovs_be32 ip;
105
106     if (nl_attr_get_size(a) != odp_action_len(a->nla_len)) {
107         ds_put_format(ds, "***bad action: length is %zu, expected %d*** ",
108                       nl_attr_get_size(a), odp_action_len(a->nla_len));
109         format_generic_odp_action(ds, a);
110         return;
111     }
112
113     switch (nl_attr_type(a)) {
114     case ODPAT_OUTPUT:
115         ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
116         break;
117     case ODPAT_CONTROLLER:
118         ds_put_format(ds, "ctl(%"PRIu32")", nl_attr_get_u32(a));
119         break;
120     case ODPAT_SET_TUNNEL:
121         ds_put_format(ds, "set_tunnel(%#"PRIx32")",
122                       ntohl(nl_attr_get_be32(a)));
123         break;
124     case ODPAT_SET_DL_TCI:
125         ds_put_format(ds, "set_tci(vid=%"PRIu16",pcp=%d)",
126                       vlan_tci_to_vid(nl_attr_get_be16(a)),
127                       vlan_tci_to_pcp(nl_attr_get_be16(a)));
128         break;
129     case ODPAT_STRIP_VLAN:
130         ds_put_format(ds, "strip_vlan");
131         break;
132     case ODPAT_SET_DL_SRC:
133         eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
134         ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
135         break;
136     case ODPAT_SET_DL_DST:
137         eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
138         ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
139         break;
140     case ODPAT_SET_NW_SRC:
141         ip = nl_attr_get_be32(a);
142         ds_put_format(ds, "set_nw_src("IP_FMT")", IP_ARGS(&ip));
143         break;
144     case ODPAT_SET_NW_DST:
145         ip = nl_attr_get_be32(a);
146         ds_put_format(ds, "set_nw_dst("IP_FMT")", IP_ARGS(&ip));
147         break;
148     case ODPAT_SET_NW_TOS:
149         ds_put_format(ds, "set_nw_tos(%"PRIu8")", nl_attr_get_u8(a));
150         break;
151     case ODPAT_SET_TP_SRC:
152         ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
153         break;
154     case ODPAT_SET_TP_DST:
155         ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
156         break;
157     case ODPAT_SET_PRIORITY:
158         ds_put_format(ds, "set_priority(%#"PRIx32")", nl_attr_get_u32(a));
159         break;
160     case ODPAT_POP_PRIORITY:
161         ds_put_cstr(ds, "pop_priority");
162         break;
163     case ODPAT_DROP_SPOOFED_ARP:
164         ds_put_cstr(ds, "drop_spoofed_arp");
165         break;
166     default:
167         format_generic_odp_action(ds, a);
168         break;
169     }
170 }
171
172 void
173 format_odp_actions(struct ds *ds, const struct nlattr *actions,
174                    unsigned int actions_len)
175 {
176     if (actions_len) {
177         const struct nlattr *a;
178         unsigned int left;
179
180         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
181             if (a != actions) {
182                 ds_put_char(ds, ',');
183             }
184             format_odp_action(ds, a);
185         }
186         if (left) {
187             ds_put_format(ds, " ***%u leftover bytes***", left);
188         }
189     } else {
190         ds_put_cstr(ds, "drop");
191     }
192 }
193
194 void
195 format_odp_flow_stats(struct ds *ds, const struct odp_flow_stats *s)
196 {
197     ds_put_format(ds, "packets:%llu, bytes:%llu, used:",
198                   (unsigned long long int) s->n_packets,
199                   (unsigned long long int) s->n_bytes);
200     if (s->used_sec) {
201         long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
202         ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
203     } else {
204         ds_put_format(ds, "never");
205     }
206 }
207
208 void
209 format_odp_flow(struct ds *ds, const struct odp_flow *f)
210 {
211     format_odp_flow_key(ds, &f->key);
212     ds_put_cstr(ds, ", ");
213     format_odp_flow_stats(ds, &f->stats);
214     ds_put_cstr(ds, ", actions:");
215     format_odp_actions(ds, f->actions, f->actions_len);
216 }
217 \f
218 void
219 odp_flow_key_from_flow(struct odp_flow_key *key, const struct flow *flow)
220 {
221     key->tun_id = flow->tun_id;
222     key->nw_src = flow->nw_src;
223     key->nw_dst = flow->nw_dst;
224     key->in_port = flow->in_port;
225     key->dl_tci = flow->vlan_tci;
226     key->dl_type = flow->dl_type;
227     key->tp_src = flow->tp_src;
228     key->tp_dst = flow->tp_dst;
229     memcpy(key->dl_src, flow->dl_src, ETH_ADDR_LEN);
230     memcpy(key->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
231     key->nw_proto = flow->nw_proto;
232     key->nw_tos = flow->nw_tos;
233 }
234
235 void
236 odp_flow_key_to_flow(const struct odp_flow_key *key, struct flow *flow)
237 {
238     memset(flow->regs, 0, sizeof flow->regs);
239     flow->tun_id = key->tun_id;
240     flow->nw_src = key->nw_src;
241     flow->nw_dst = key->nw_dst;
242     flow->in_port = key->in_port;
243     flow->vlan_tci = key->dl_tci;
244     flow->dl_type = key->dl_type;
245     flow->tp_src = key->tp_src;
246     flow->tp_dst = key->tp_dst;
247     memcpy(flow->dl_src, key->dl_src, ETH_ADDR_LEN);
248     memcpy(flow->dl_dst, key->dl_dst, ETH_ADDR_LEN);
249     flow->nw_proto = key->nw_proto;
250     flow->nw_tos = key->nw_tos;
251 }