2 * Copyright (c) 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "xflow-util.h"
23 #include "dynamic-string.h"
30 xflow_actions_add(struct xflow_actions *actions, uint16_t type)
32 union xflow_action *a;
33 if (actions->n_actions < MAX_XFLOW_ACTIONS) {
34 a = &actions->actions[actions->n_actions++];
36 COVERAGE_INC(xflow_overflow);
37 actions->n_actions = MAX_XFLOW_ACTIONS + 1;
38 a = &actions->actions[MAX_XFLOW_ACTIONS - 1];
40 memset(a, 0, sizeof *a);
46 format_xflow_key(struct ds *ds, const struct xflow_key *key)
48 ds_put_format(ds, "in_port%04x", key->in_port);
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));
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));
63 format_xflow_action(struct ds *ds, const union xflow_action *a)
67 ds_put_format(ds, "%"PRIu16, a->output.port);
69 case XFLOWAT_OUTPUT_GROUP:
70 ds_put_format(ds, "g%"PRIu16, a->output_group.group);
72 case XFLOWAT_CONTROLLER:
73 ds_put_format(ds, "ctl(%"PRIu32")", a->controller.arg);
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));
79 case XFLOWAT_STRIP_VLAN:
80 ds_put_format(ds, "strip_vlan");
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));
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));
90 case XFLOWAT_SET_NW_SRC:
91 ds_put_format(ds, "set_nw_src("IP_FMT")",
92 IP_ARGS(&a->nw_addr.nw_addr));
94 case XFLOWAT_SET_NW_DST:
95 ds_put_format(ds, "set_nw_dst("IP_FMT")",
96 IP_ARGS(&a->nw_addr.nw_addr));
98 case XFLOWAT_SET_NW_TOS:
99 ds_put_format(ds, "set_nw_tos(%"PRIu8")", a->nw_tos.nw_tos);
101 case XFLOWAT_SET_TP_SRC:
102 ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(a->tp_port.tp_port));
104 case XFLOWAT_SET_TP_DST:
105 ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(a->tp_port.tp_port));
108 ds_put_format(ds, "***bad action %"PRIu16"***", a->type);
114 format_xflow_actions(struct ds *ds, const union xflow_action *actions,
118 for (i = 0; i < n_actions; i++) {
120 ds_put_char(ds, ',');
122 format_xflow_action(ds, &actions[i]);
125 ds_put_cstr(ds, "drop");
130 format_xflow_flow_stats(struct ds *ds, const struct xflow_flow_stats *s)
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);
136 long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
137 ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
139 ds_put_format(ds, "never");
144 format_xflow_flow(struct ds *ds, const struct xflow_flow *f)
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);
154 xflow_key_from_flow(struct xflow_key *key, const struct flow *flow)
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);
162 uint16_t vid = flow->dl_vlan & htons(VLAN_VID_MASK);
163 uint16_t pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT)
165 key->dl_tci = vid | pcp | htons(XFLOW_TCI_PRESENT);
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;
177 xflow_key_to_flow(const struct xflow_key *key, struct flow *flow)
179 flow->nw_src = key->nw_src;
180 flow->nw_dst = key->nw_dst;
181 flow->in_port = key->in_port;
183 flow->dl_vlan = htons(vlan_tci_to_vid(key->dl_tci));
184 flow->dl_vlan_pcp = vlan_tci_to_pcp(key->dl_tci);
186 flow->dl_vlan = htons(OFP_VLAN_NONE);
187 flow->dl_vlan_pcp = 0;
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;