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