130d55de77f2d99930cb76afd1d3cf5d544878d2
[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 "coverage.h"
23 #include "dynamic-string.h"
24 #include "flow.h"
25 #include "packets.h"
26 #include "timeval.h"
27 #include "util.h"
28
29 union odp_action *
30 odp_actions_add(struct odp_actions *actions, uint16_t type)
31 {
32     union odp_action *a;
33     size_t idx;
34
35     idx = actions->n_actions++ & (MAX_ODP_ACTIONS - 1);
36     a = &actions->actions[idx];
37     memset(a, 0, sizeof *a);
38     a->type = type;
39     return a;
40 }
41
42 void
43 format_odp_flow_key(struct ds *ds, const struct odp_flow_key *key)
44 {
45     ds_put_format(ds, "in_port%04x tci(", key->in_port);
46     if (key->dl_tci) {
47         ds_put_format(ds, "vlan%"PRIu16",pcp%d",
48                       vlan_tci_to_vid(key->dl_tci),
49                       vlan_tci_to_pcp(key->dl_tci));
50     } else {
51         ds_put_char(ds, '0');
52     }
53     ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" type%04x "
54                   "proto%"PRId8" tos%"PRIu8" ip"IP_FMT"->"IP_FMT" port%d->%d",
55                   ETH_ADDR_ARGS(key->dl_src), ETH_ADDR_ARGS(key->dl_dst),
56                   ntohs(key->dl_type), key->nw_proto, key->nw_tos,
57                   IP_ARGS(&key->nw_src), IP_ARGS(&key->nw_dst),
58                   ntohs(key->tp_src), ntohs(key->tp_dst));
59 }
60
61 void
62 format_odp_action(struct ds *ds, const union odp_action *a)
63 {
64     switch (a->type) {
65     case ODPAT_OUTPUT:
66         ds_put_format(ds, "%"PRIu16, a->output.port);
67         break;
68     case ODPAT_CONTROLLER:
69         ds_put_format(ds, "ctl(%"PRIu32")", a->controller.arg);
70         break;
71     case ODPAT_SET_TUNNEL:
72         ds_put_format(ds, "set_tunnel(0x%08"PRIx32")", ntohl(a->tunnel.tun_id));
73         break;
74     case ODPAT_SET_DL_TCI: {
75         int vid = vlan_tci_to_vid(a->dl_tci.tci);
76         int pcp = vlan_tci_to_pcp(a->dl_tci.tci);
77
78         switch (ntohs(a->dl_tci.mask)) {
79         case VLAN_VID_MASK:
80             ds_put_format(ds, "set_tci(vlan=%d)", vid);
81             break;
82         case VLAN_PCP_MASK:
83             ds_put_format(ds, "set_tci(pcp=%d)", pcp);
84             break;
85         case VLAN_VID_MASK | VLAN_PCP_MASK:
86             ds_put_format(ds, "set_tci(vlan=%d,pcp=%d)", vid, pcp);
87             break;
88         default:
89             ds_put_format(ds, "set_tci(tci=%04"PRIx16",mask=%04"PRIx16")",
90                           ntohs(a->dl_tci.tci), ntohs(a->dl_tci.mask));
91             break;
92         }
93     }
94         break;
95     case ODPAT_STRIP_VLAN:
96         ds_put_format(ds, "strip_vlan");
97         break;
98     case ODPAT_SET_DL_SRC:
99         ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")",
100                ETH_ADDR_ARGS(a->dl_addr.dl_addr));
101         break;
102     case ODPAT_SET_DL_DST:
103         ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")",
104                ETH_ADDR_ARGS(a->dl_addr.dl_addr));
105         break;
106     case ODPAT_SET_NW_SRC:
107         ds_put_format(ds, "set_nw_src("IP_FMT")",
108                       IP_ARGS(&a->nw_addr.nw_addr));
109         break;
110     case ODPAT_SET_NW_DST:
111         ds_put_format(ds, "set_nw_dst("IP_FMT")",
112                       IP_ARGS(&a->nw_addr.nw_addr));
113         break;
114     case ODPAT_SET_NW_TOS:
115         ds_put_format(ds, "set_nw_tos(%"PRIu8")", a->nw_tos.nw_tos);
116         break;
117     case ODPAT_SET_TP_SRC:
118         ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(a->tp_port.tp_port));
119         break;
120     case ODPAT_SET_TP_DST:
121         ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(a->tp_port.tp_port));
122         break;
123     case ODPAT_SET_PRIORITY:
124         ds_put_format(ds, "set_priority(0x%"PRIx32")", a->priority.priority);
125         break;
126     case ODPAT_POP_PRIORITY:
127         ds_put_cstr(ds, "pop_priority");
128         break;
129     case ODPAT_DROP_SPOOFED_ARP:
130         ds_put_cstr(ds, "drop_spoofed_arp");
131         break;
132     default:
133         ds_put_format(ds, "***bad action 0x%"PRIx16"***", a->type);
134         break;
135     }
136 }
137
138 void
139 format_odp_actions(struct ds *ds, const union odp_action *actions,
140                    size_t n_actions)
141 {
142     size_t i;
143     for (i = 0; i < n_actions; i++) {
144         if (i) {
145             ds_put_char(ds, ',');
146         }
147         format_odp_action(ds, &actions[i]);
148     }
149     if (!n_actions) {
150         ds_put_cstr(ds, "drop");
151     }
152 }
153
154 void
155 format_odp_flow_stats(struct ds *ds, const struct odp_flow_stats *s)
156 {
157     ds_put_format(ds, "packets:%llu, bytes:%llu, used:",
158                   (unsigned long long int) s->n_packets,
159                   (unsigned long long int) s->n_bytes);
160     if (s->used_sec) {
161         long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
162         ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
163     } else {
164         ds_put_format(ds, "never");
165     }
166 }
167
168 void
169 format_odp_flow(struct ds *ds, const struct odp_flow *f)
170 {
171     format_odp_flow_key(ds, &f->key);
172     ds_put_cstr(ds, ", ");
173     format_odp_flow_stats(ds, &f->stats);
174     ds_put_cstr(ds, ", actions:");
175     format_odp_actions(ds, f->actions, f->n_actions);
176 }
177 \f
178 void
179 odp_flow_key_from_flow(struct odp_flow_key *key, const struct flow *flow)
180 {
181     key->tun_id = flow->tun_id;
182     key->nw_src = flow->nw_src;
183     key->nw_dst = flow->nw_dst;
184     key->in_port = flow->in_port;
185     if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
186         key->dl_tci = htons(0);
187     } else {
188         uint16_t vid = flow->dl_vlan & htons(VLAN_VID_MASK);
189         uint16_t pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT)
190                              & VLAN_PCP_MASK);
191         key->dl_tci = vid | pcp | htons(ODP_TCI_PRESENT);
192     }
193     key->dl_type = flow->dl_type;
194     key->tp_src = flow->tp_src;
195     key->tp_dst = flow->tp_dst;
196     memcpy(key->dl_src, flow->dl_src, ETH_ADDR_LEN);
197     memcpy(key->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
198     key->nw_proto = flow->nw_proto;
199     key->nw_tos = flow->nw_tos;
200 }
201
202 void
203 odp_flow_key_to_flow(const struct odp_flow_key *key, struct flow *flow)
204 {
205     flow->tun_id = key->tun_id;
206     flow->nw_src = key->nw_src;
207     flow->nw_dst = key->nw_dst;
208     flow->in_port = key->in_port;
209     if (key->dl_tci) {
210         flow->dl_vlan = htons(vlan_tci_to_vid(key->dl_tci));
211         flow->dl_vlan_pcp = vlan_tci_to_pcp(key->dl_tci);
212     } else {
213         flow->dl_vlan = htons(OFP_VLAN_NONE);
214         flow->dl_vlan_pcp = 0;
215     }
216     flow->dl_type = key->dl_type;
217     flow->tp_src = key->tp_src;
218     flow->tp_dst = key->tp_dst;
219     memcpy(flow->dl_src, key->dl_src, ETH_ADDR_LEN);
220     memcpy(flow->dl_dst, key->dl_dst, ETH_ADDR_LEN);
221     flow->nw_proto = key->nw_proto;
222     flow->nw_tos = key->nw_tos;
223 }