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