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.
23 #include "dynamic-string.h"
30 odp_actions_add(struct odp_actions *actions, uint16_t type)
35 idx = actions->n_actions++ & (MAX_ODP_ACTIONS - 1);
36 a = &actions->actions[idx];
37 memset(a, 0, sizeof *a);
43 format_odp_flow_key(struct ds *ds, const struct odp_flow_key *key)
45 ds_put_format(ds, "in_port%04x tci(", key->in_port);
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));
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));
62 format_odp_action(struct ds *ds, const union odp_action *a)
66 ds_put_format(ds, "%"PRIu16, a->output.port);
68 case ODPAT_CONTROLLER:
69 ds_put_format(ds, "ctl(%"PRIu32")", a->controller.arg);
71 case ODPAT_SET_TUNNEL:
72 ds_put_format(ds, "set_tunnel(0x%08"PRIx32")", ntohl(a->tunnel.tun_id));
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);
78 ds_put_cstr(ds, "set_tci(");
79 switch (ntohs(a->dl_tci.mask)) {
81 ds_put_format(ds, "set_tci(vlan=%d)", vid);
84 ds_put_format(ds, "set_tci(pcp=%d)", pcp);
86 case VLAN_VID_MASK | VLAN_PCP_MASK:
87 ds_put_format(ds, "set_tci(vlan=%d,pcp=%d)", vid, pcp);
90 ds_put_format(ds, "set_tci(tci=%04"PRIx16",mask=%04"PRIx16")",
91 ntohs(a->dl_tci.tci), ntohs(a->dl_tci.mask));
96 case ODPAT_STRIP_VLAN:
97 ds_put_format(ds, "strip_vlan");
99 case ODPAT_SET_DL_SRC:
100 ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")",
101 ETH_ADDR_ARGS(a->dl_addr.dl_addr));
103 case ODPAT_SET_DL_DST:
104 ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")",
105 ETH_ADDR_ARGS(a->dl_addr.dl_addr));
107 case ODPAT_SET_NW_SRC:
108 ds_put_format(ds, "set_nw_src("IP_FMT")",
109 IP_ARGS(&a->nw_addr.nw_addr));
111 case ODPAT_SET_NW_DST:
112 ds_put_format(ds, "set_nw_dst("IP_FMT")",
113 IP_ARGS(&a->nw_addr.nw_addr));
115 case ODPAT_SET_NW_TOS:
116 ds_put_format(ds, "set_nw_tos(%"PRIu8")", a->nw_tos.nw_tos);
118 case ODPAT_SET_TP_SRC:
119 ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(a->tp_port.tp_port));
121 case ODPAT_SET_TP_DST:
122 ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(a->tp_port.tp_port));
124 case ODPAT_SET_PRIORITY:
125 ds_put_format(ds, "set_priority(0x%"PRIx32")", a->priority.priority);
127 case ODPAT_POP_PRIORITY:
128 ds_put_cstr(ds, "pop_priority");
130 case ODPAT_DROP_SPOOFED_ARP:
131 ds_put_cstr(ds, "drop_spoofed_arp");
134 ds_put_format(ds, "***bad action 0x%"PRIx16"***", a->type);
140 format_odp_actions(struct ds *ds, const union odp_action *actions,
144 for (i = 0; i < n_actions; i++) {
146 ds_put_char(ds, ',');
148 format_odp_action(ds, &actions[i]);
151 ds_put_cstr(ds, "drop");
156 format_odp_flow_stats(struct ds *ds, const struct odp_flow_stats *s)
158 ds_put_format(ds, "packets:%llu, bytes:%llu, used:",
159 (unsigned long long int) s->n_packets,
160 (unsigned long long int) s->n_bytes);
162 long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
163 ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
165 ds_put_format(ds, "never");
170 format_odp_flow(struct ds *ds, const struct odp_flow *f)
172 format_odp_flow_key(ds, &f->key);
173 ds_put_cstr(ds, ", ");
174 format_odp_flow_stats(ds, &f->stats);
175 ds_put_cstr(ds, ", actions:");
176 format_odp_actions(ds, f->actions, f->n_actions);
180 odp_flow_key_from_flow(struct odp_flow_key *key, const struct flow *flow)
182 key->tun_id = flow->tun_id;
183 key->nw_src = flow->nw_src;
184 key->nw_dst = flow->nw_dst;
185 key->in_port = flow->in_port;
186 if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
187 key->dl_tci = htons(0);
189 uint16_t vid = flow->dl_vlan & htons(VLAN_VID_MASK);
190 uint16_t pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT)
192 key->dl_tci = vid | pcp | htons(ODP_TCI_PRESENT);
194 key->dl_type = flow->dl_type;
195 key->tp_src = flow->tp_src;
196 key->tp_dst = flow->tp_dst;
197 memcpy(key->dl_src, flow->dl_src, ETH_ADDR_LEN);
198 memcpy(key->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
199 key->nw_proto = flow->nw_proto;
200 key->nw_tos = flow->nw_tos;
204 odp_flow_key_to_flow(const struct odp_flow_key *key, struct flow *flow)
206 flow->tun_id = key->tun_id;
207 flow->nw_src = key->nw_src;
208 flow->nw_dst = key->nw_dst;
209 flow->in_port = key->in_port;
211 flow->dl_vlan = htons(vlan_tci_to_vid(key->dl_tci));
212 flow->dl_vlan_pcp = vlan_tci_to_pcp(key->dl_tci);
214 flow->dl_vlan = htons(OFP_VLAN_NONE);
215 flow->dl_vlan_pcp = 0;
217 flow->dl_type = key->dl_type;
218 flow->tp_src = key->tp_src;
219 flow->tp_dst = key->tp_dst;
220 memcpy(flow->dl_src, key->dl_src, ETH_ADDR_LEN);
221 memcpy(flow->dl_dst, key->dl_dst, ETH_ADDR_LEN);
222 flow->nw_proto = key->nw_proto;
223 flow->nw_tos = key->nw_tos;