Fix typos in comments.
[sliver-openvswitch.git] / lib / xflow-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 "xflow-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 xflow_action *
30 xflow_actions_add(struct xflow_actions *actions, uint16_t type)
31 {
32     union xflow_action *a;
33     size_t idx;
34
35     idx = actions->n_actions++ & (MAX_XFLOW_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_xflow_key(struct ds *ds, const struct xflow_key *key)
44 {
45     ds_put_format(ds, "tunnel%"PRIx32":in_port%04x",
46                   key->tun_id, key->in_port);
47     if (key->dl_tci) {
48         ds_put_format(ds, ":vlan%"PRIu16":pcp%d",
49                       vlan_tci_to_vid(key->dl_tci),
50                       vlan_tci_to_pcp(key->dl_tci));
51     }
52     ds_put_format(ds, " mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" type%04x "
53                   "proto%"PRId8" tos%"PRIu8" ip"IP_FMT"->"IP_FMT" port%d->%d",
54                   ETH_ADDR_ARGS(key->dl_src), ETH_ADDR_ARGS(key->dl_dst),
55                   ntohs(key->dl_type), key->nw_proto, key->nw_tos,
56                   IP_ARGS(&key->nw_src), IP_ARGS(&key->nw_dst),
57                   ntohs(key->tp_src), ntohs(key->tp_dst));
58 }
59
60 void
61 format_xflow_action(struct ds *ds, const union xflow_action *a)
62 {
63     switch (a->type) {
64     case XFLOWAT_OUTPUT:
65         ds_put_format(ds, "%"PRIu16, a->output.port);
66         break;
67     case XFLOWAT_OUTPUT_GROUP:
68         ds_put_format(ds, "g%"PRIu16, a->output_group.group);
69         break;
70     case XFLOWAT_CONTROLLER:
71         ds_put_format(ds, "ctl(%"PRIu32")", a->controller.arg);
72         break;
73     case XFLOWAT_SET_TUNNEL:
74         ds_put_format(ds, "set_tunnel(0x%08"PRIx32")", ntohl(a->tunnel.tun_id));
75         break;
76     case XFLOWAT_SET_DL_TCI:
77         ds_put_format(ds, "set_tci(%04"PRIx16",mask=%04"PRIx16")",
78                       ntohs(a->dl_tci.tci), ntohs(a->dl_tci.mask));
79         break;
80     case XFLOWAT_STRIP_VLAN:
81         ds_put_format(ds, "strip_vlan");
82         break;
83     case XFLOWAT_SET_DL_SRC:
84         ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")",
85                ETH_ADDR_ARGS(a->dl_addr.dl_addr));
86         break;
87     case XFLOWAT_SET_DL_DST:
88         ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")",
89                ETH_ADDR_ARGS(a->dl_addr.dl_addr));
90         break;
91     case XFLOWAT_SET_NW_SRC:
92         ds_put_format(ds, "set_nw_src("IP_FMT")",
93                       IP_ARGS(&a->nw_addr.nw_addr));
94         break;
95     case XFLOWAT_SET_NW_DST:
96         ds_put_format(ds, "set_nw_dst("IP_FMT")",
97                       IP_ARGS(&a->nw_addr.nw_addr));
98         break;
99     case XFLOWAT_SET_NW_TOS:
100         ds_put_format(ds, "set_nw_tos(%"PRIu8")", a->nw_tos.nw_tos);
101         break;
102     case XFLOWAT_SET_TP_SRC:
103         ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(a->tp_port.tp_port));
104         break;
105     case XFLOWAT_SET_TP_DST:
106         ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(a->tp_port.tp_port));
107         break;
108     case XFLOWAT_SET_PRIORITY:
109         ds_put_format(ds, "set_priority(0x%"PRIx32")", a->priority.priority);
110         break;
111     case XFLOWAT_POP_PRIORITY:
112         ds_put_cstr(ds, "pop_priority");
113         break;
114     case XFLOWAT_DROP_SPOOFED_ARP:
115         ds_put_cstr(ds, "drop_spoofed_arp");
116         break;
117     default:
118         ds_put_format(ds, "***bad action 0x%"PRIx16"***", a->type);
119         break;
120     }
121 }
122
123 void
124 format_xflow_actions(struct ds *ds, const union xflow_action *actions,
125                    size_t n_actions)
126 {
127     size_t i;
128     for (i = 0; i < n_actions; i++) {
129         if (i) {
130             ds_put_char(ds, ',');
131         }
132         format_xflow_action(ds, &actions[i]);
133     }
134     if (!n_actions) {
135         ds_put_cstr(ds, "drop");
136     }
137 }
138
139 void
140 format_xflow_flow_stats(struct ds *ds, const struct xflow_flow_stats *s)
141 {
142     ds_put_format(ds, "packets:%llu, bytes:%llu, used:",
143                   (unsigned long long int) s->n_packets,
144                   (unsigned long long int) s->n_bytes);
145     if (s->used_sec) {
146         long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
147         ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
148     } else {
149         ds_put_format(ds, "never");
150     }
151 }
152
153 void
154 format_xflow_flow(struct ds *ds, const struct xflow_flow *f)
155 {
156     format_xflow_key(ds, &f->key);
157     ds_put_cstr(ds, ", ");
158     format_xflow_flow_stats(ds, &f->stats);
159     ds_put_cstr(ds, ", actions:");
160     format_xflow_actions(ds, f->actions, f->n_actions);
161 }
162 \f
163 void
164 xflow_key_from_flow(struct xflow_key *key, const struct flow *flow)
165 {
166     key->tun_id = flow->tun_id;
167     key->nw_src = flow->nw_src;
168     key->nw_dst = flow->nw_dst;
169     key->in_port = ofp_port_to_xflow_port(flow->in_port);
170     if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
171         key->dl_tci = htons(0);
172     } else {
173         uint16_t vid = flow->dl_vlan & htons(VLAN_VID_MASK);
174         uint16_t pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT)
175                              & VLAN_PCP_MASK);
176         key->dl_tci = vid | pcp | htons(XFLOW_TCI_PRESENT);
177     }
178     key->dl_type = flow->dl_type;
179     key->tp_src = flow->tp_src;
180     key->tp_dst = flow->tp_dst;
181     memcpy(key->dl_src, flow->dl_src, ETH_ADDR_LEN);
182     memcpy(key->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
183     key->nw_proto = flow->nw_proto;
184     key->nw_tos = flow->nw_tos;
185 }
186
187 void
188 xflow_key_to_flow(const struct xflow_key *key, struct flow *flow)
189 {
190     flow->wildcards = 0;
191     flow->priority = 0xffff;
192     flow->tun_id = key->tun_id;
193     flow->nw_src = key->nw_src;
194     flow->nw_dst = key->nw_dst;
195     flow->in_port = xflow_port_to_ofp_port(key->in_port);
196     if (key->dl_tci) {
197         flow->dl_vlan = htons(vlan_tci_to_vid(key->dl_tci));
198         flow->dl_vlan_pcp = vlan_tci_to_pcp(key->dl_tci);
199     } else {
200         flow->dl_vlan = htons(OFP_VLAN_NONE);
201         flow->dl_vlan_pcp = 0;
202     }
203     flow->dl_type = key->dl_type;
204     flow->tp_src = key->tp_src;
205     flow->tp_dst = key->tp_dst;
206     memcpy(flow->dl_src, key->dl_src, ETH_ADDR_LEN);
207     memcpy(flow->dl_dst, key->dl_dst, ETH_ADDR_LEN);
208     flow->nw_proto = key->nw_proto;
209     flow->nw_tos = key->nw_tos;
210 }