]> git.proxmox.com Git - ovs.git/blame - lib/ofp-print.c
util: New function bitwise_zero().
[ovs.git] / lib / ofp-print.c
CommitLineData
064af421 1/*
73dbf4ab 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "ofp-print.h"
064af421
BP
19
20#include <errno.h>
21#include <inttypes.h>
7f3adc00 22#include <sys/types.h>
064af421
BP
23#include <netinet/in.h>
24#include <sys/wait.h>
25#include <stdarg.h>
26#include <stdlib.h>
27#include <ctype.h>
28
daff3353 29#include "bundle.h"
10a24935 30#include "byte-order.h"
064af421
BP
31#include "compiler.h"
32#include "dynamic-string.h"
33#include "flow.h"
75a75043 34#include "learn.h"
53ddd40a 35#include "multipath.h"
7fa91113 36#include "nx-match.h"
90bf1e07 37#include "ofp-errors.h"
69b6be19 38#include "ofp-util.h"
064af421
BP
39#include "ofpbuf.h"
40#include "openflow/openflow.h"
41#include "openflow/nicira-ext.h"
42#include "packets.h"
43#include "pcap.h"
e41a9130 44#include "type-props.h"
c4617b3c 45#include "unaligned.h"
064af421
BP
46#include "util.h"
47
d2805da2 48static void ofp_print_queue_name(struct ds *string, uint32_t port);
90bf1e07 49static void ofp_print_error(struct ds *, enum ofperr);
7fa91113 50
064af421
BP
51
52/* Returns a string that represents the contents of the Ethernet frame in the
897a8e07 53 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
064af421 54char *
c499c75d 55ofp_packet_to_string(const void *data, size_t len)
064af421
BP
56{
57 struct ds ds = DS_EMPTY_INITIALIZER;
58 struct ofpbuf buf;
897a8e07 59 struct flow flow;
064af421 60
0bc9407d 61 ofpbuf_use_const(&buf, data, len);
897a8e07
EJ
62 flow_extract(&buf, 0, 0, 0, &flow);
63 flow_format(&ds, &flow);
e50abca5
EJ
64
65 if (buf.l7) {
66 if (flow.nw_proto == IPPROTO_TCP) {
67 struct tcp_header *th = buf.l4;
68 ds_put_format(&ds, " tcp_csum:%"PRIx16,
69 ntohs(th->tcp_csum));
70 } else if (flow.nw_proto == IPPROTO_UDP) {
71 struct udp_header *uh = buf.l4;
72 ds_put_format(&ds, " udp_csum:%"PRIx16,
73 ntohs(uh->udp_csum));
74 }
75 }
76
897a8e07 77 ds_put_char(&ds, '\n');
064af421 78
064af421
BP
79 return ds_cstr(&ds);
80}
81
064af421 82static void
65120a8a 83ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
d1e2cf21 84 int verbosity)
064af421 85{
65120a8a
EJ
86 struct ofputil_packet_in pin;
87 int error;
5d6c3af0 88 int i;
064af421 89
65120a8a
EJ
90 error = ofputil_decode_packet_in(&pin, oh);
91 if (error) {
92 ofp_print_error(string, error);
93 return;
94 }
95
54834960
EJ
96 if (pin.table_id) {
97 ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
98 }
99
100 if (pin.cookie) {
101 ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
102 }
103
65120a8a 104 ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
5d6c3af0
EJ
105 ofputil_format_port(pin.fmd.in_port, string);
106
107 if (pin.fmd.tun_id_mask) {
108 ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
109 if (pin.fmd.tun_id_mask != htonll(UINT64_MAX)) {
110 ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.tun_id_mask));
111 }
112 }
113
114 for (i = 0; i < FLOW_N_REGS; i++) {
115 if (pin.fmd.reg_masks[i]) {
116 ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
117 if (pin.fmd.reg_masks[i] != UINT32_MAX) {
118 ds_put_format(string, "/0x%"PRIx32, pin.fmd.reg_masks[i]);
119 }
120 }
121 }
064af421 122
f0fd1a17
PS
123 switch (pin.reason) {
124 case OFPR_NO_MATCH:
125 ds_put_cstr(string, " (via no_match)");
126 break;
127 case OFPR_ACTION:
064af421 128 ds_put_cstr(string, " (via action)");
f0fd1a17
PS
129 break;
130 case OFPR_INVALID_TTL:
131 ds_put_cstr(string, " (via invalid_ttl)");
132 break;
133 default:
65120a8a 134 ds_put_format(string, " (***reason %"PRIu8"***)", pin.reason);
f0fd1a17 135 break;
65120a8a 136 }
064af421 137
65120a8a
EJ
138 ds_put_format(string, " data_len=%zu", pin.packet_len);
139 if (pin.buffer_id == UINT32_MAX) {
064af421 140 ds_put_format(string, " (unbuffered)");
65120a8a 141 if (pin.total_len != pin.packet_len) {
064af421 142 ds_put_format(string, " (***total_len != data_len***)");
65120a8a 143 }
064af421 144 } else {
65120a8a
EJ
145 ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
146 if (pin.total_len < pin.packet_len) {
064af421 147 ds_put_format(string, " (***total_len < data_len***)");
65120a8a 148 }
064af421
BP
149 }
150 ds_put_char(string, '\n');
151
152 if (verbosity > 0) {
65120a8a 153 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
064af421
BP
154 ds_put_cstr(string, packet);
155 free(packet);
156 }
157}
158
96fc46e8
BP
159static void
160print_note(struct ds *string, const struct nx_action_note *nan)
161{
162 size_t len;
163 size_t i;
164
165 ds_put_cstr(string, "note:");
166 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
167 for (i = 0; i < len; i++) {
168 if (i) {
169 ds_put_char(string, '.');
170 }
171 ds_put_format(string, "%02"PRIx8, nan->note[i]);
172 }
173}
174
064af421 175static void
38f2e360
BP
176ofp_print_action(struct ds *s, const union ofp_action *a,
177 enum ofputil_action_code code)
064af421 178{
38f2e360
BP
179 const struct ofp_action_enqueue *oae;
180 const struct ofp_action_dl_addr *oada;
181 const struct nx_action_set_tunnel64 *nast64;
182 const struct nx_action_set_tunnel *nast;
183 const struct nx_action_set_queue *nasq;
184 const struct nx_action_resubmit *nar;
185 const struct nx_action_reg_move *move;
186 const struct nx_action_reg_load *load;
187 const struct nx_action_multipath *nam;
188 const struct nx_action_autopath *naa;
f694937d 189 const struct nx_action_output_reg *naor;
38f2e360
BP
190 uint16_t port;
191
192 switch (code) {
193 case OFPUTIL_OFPAT_OUTPUT:
194 port = ntohs(a->output.port);
195 if (port < OFPP_MAX) {
196 ds_put_format(s, "output:%"PRIu16, port);
197 } else {
39dc9082 198 ofputil_format_port(port, s);
38f2e360
BP
199 if (port == OFPP_CONTROLLER) {
200 if (a->output.max_len != htons(0)) {
201 ds_put_format(s, ":%"PRIu16, ntohs(a->output.max_len));
202 } else {
203 ds_put_cstr(s, ":all");
204 }
205 }
206 }
207 break;
f393f81e 208
38f2e360
BP
209 case OFPUTIL_OFPAT_ENQUEUE:
210 oae = (const struct ofp_action_enqueue *) a;
211 ds_put_format(s, "enqueue:");
39dc9082 212 ofputil_format_port(ntohs(oae->port), s);
38f2e360
BP
213 ds_put_format(s, "q%"PRIu32, ntohl(oae->queue_id));
214 break;
e41a9130 215
38f2e360
BP
216 case OFPUTIL_OFPAT_SET_VLAN_VID:
217 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
218 ntohs(a->vlan_vid.vlan_vid));
219 break;
eedc0097 220
38f2e360
BP
221 case OFPUTIL_OFPAT_SET_VLAN_PCP:
222 ds_put_format(s, "mod_vlan_pcp:%"PRIu8, a->vlan_pcp.vlan_pcp);
223 break;
96fc46e8 224
38f2e360
BP
225 case OFPUTIL_OFPAT_STRIP_VLAN:
226 ds_put_cstr(s, "strip_vlan");
227 break;
064af421 228
38f2e360
BP
229 case OFPUTIL_OFPAT_SET_DL_SRC:
230 oada = (const struct ofp_action_dl_addr *) a;
231 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
232 ETH_ADDR_ARGS(oada->dl_addr));
233 break;
064af421 234
38f2e360
BP
235 case OFPUTIL_OFPAT_SET_DL_DST:
236 oada = (const struct ofp_action_dl_addr *) a;
237 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
238 ETH_ADDR_ARGS(oada->dl_addr));
239 break;
064af421 240
38f2e360
BP
241 case OFPUTIL_OFPAT_SET_NW_SRC:
242 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
243 break;
d295e8e9 244
38f2e360
BP
245 case OFPUTIL_OFPAT_SET_NW_DST:
246 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
064af421 247 break;
064af421 248
38f2e360
BP
249 case OFPUTIL_OFPAT_SET_NW_TOS:
250 ds_put_format(s, "mod_nw_tos:%d", a->nw_tos.nw_tos);
c4d279ab 251 break;
c4d279ab 252
38f2e360
BP
253 case OFPUTIL_OFPAT_SET_TP_SRC:
254 ds_put_format(s, "mod_tp_src:%d", ntohs(a->tp_port.tp_port));
064af421 255 break;
064af421 256
38f2e360
BP
257 case OFPUTIL_OFPAT_SET_TP_DST:
258 ds_put_format(s, "mod_tp_dst:%d", ntohs(a->tp_port.tp_port));
064af421 259 break;
064af421 260
38f2e360
BP
261 case OFPUTIL_NXAST_RESUBMIT:
262 nar = (struct nx_action_resubmit *)a;
263 ds_put_format(s, "resubmit:");
39dc9082 264 ofputil_format_port(ntohs(nar->in_port), s);
064af421
BP
265 break;
266
29901626
BP
267 case OFPUTIL_NXAST_RESUBMIT_TABLE:
268 nar = (struct nx_action_resubmit *)a;
269 ds_put_format(s, "resubmit(");
270 if (nar->in_port != htons(OFPP_IN_PORT)) {
39dc9082 271 ofputil_format_port(ntohs(nar->in_port), s);
29901626
BP
272 }
273 ds_put_char(s, ',');
274 if (nar->table != 255) {
275 ds_put_format(s, "%"PRIu8, nar->table);
276 }
277 ds_put_char(s, ')');
278 break;
279
38f2e360
BP
280 case OFPUTIL_NXAST_SET_TUNNEL:
281 nast = (struct nx_action_set_tunnel *)a;
282 ds_put_format(s, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
064af421 283 break;
064af421 284
38f2e360
BP
285 case OFPUTIL_NXAST_SET_QUEUE:
286 nasq = (struct nx_action_set_queue *)a;
287 ds_put_format(s, "set_queue:%u", ntohl(nasq->queue_id));
064af421 288 break;
064af421 289
38f2e360
BP
290 case OFPUTIL_NXAST_POP_QUEUE:
291 ds_put_cstr(s, "pop_queue");
064af421 292 break;
064af421 293
38f2e360
BP
294 case OFPUTIL_NXAST_NOTE:
295 print_note(s, (const struct nx_action_note *) a);
064af421 296 break;
064af421 297
38f2e360
BP
298 case OFPUTIL_NXAST_REG_MOVE:
299 move = (const struct nx_action_reg_move *) a;
300 nxm_format_reg_move(move, s);
959a2ecd 301 break;
959a2ecd 302
38f2e360
BP
303 case OFPUTIL_NXAST_REG_LOAD:
304 load = (const struct nx_action_reg_load *) a;
305 nxm_format_reg_load(load, s);
064af421 306 break;
064af421 307
38f2e360
BP
308 case OFPUTIL_NXAST_SET_TUNNEL64:
309 nast64 = (const struct nx_action_set_tunnel64 *) a;
310 ds_put_format(s, "set_tunnel64:%#"PRIx64,
311 ntohll(nast64->tun_id));
064af421 312 break;
064af421 313
38f2e360
BP
314 case OFPUTIL_NXAST_MULTIPATH:
315 nam = (const struct nx_action_multipath *) a;
316 multipath_format(nam, s);
317 break;
318
319 case OFPUTIL_NXAST_AUTOPATH:
320 naa = (const struct nx_action_autopath *)a;
321 ds_put_format(s, "autopath(%u,", ntohl(naa->id));
322 nxm_format_field_bits(s, ntohl(naa->dst),
323 nxm_decode_ofs(naa->ofs_nbits),
324 nxm_decode_n_bits(naa->ofs_nbits));
325 ds_put_char(s, ')');
064af421 326 break;
064af421 327
daff3353 328 case OFPUTIL_NXAST_BUNDLE:
a368bb53 329 case OFPUTIL_NXAST_BUNDLE_LOAD:
daff3353
EJ
330 bundle_format((const struct nx_action_bundle *) a, s);
331 break;
332
f694937d
EJ
333 case OFPUTIL_NXAST_OUTPUT_REG:
334 naor = (const struct nx_action_output_reg *) a;
335 ds_put_cstr(s, "output:");
336 nxm_format_field_bits(s, ntohl(naor->src),
337 nxm_decode_ofs(naor->ofs_nbits),
338 nxm_decode_n_bits(naor->ofs_nbits));
0c58c0c4 339 break;
f694937d 340
75a75043
BP
341 case OFPUTIL_NXAST_LEARN:
342 learn_format((const struct nx_action_learn *) a, s);
343 break;
344
f0fd1a17
PS
345 case OFPUTIL_NXAST_DEC_TTL:
346 ds_put_cstr(s, "dec_ttl");
347 break;
348
848e8809
EJ
349 case OFPUTIL_NXAST_EXIT:
350 ds_put_cstr(s, "exit");
351 break;
352
064af421 353 default:
064af421
BP
354 break;
355 }
064af421
BP
356}
357
d295e8e9 358void
b4b8c781
BP
359ofp_print_actions(struct ds *string, const union ofp_action *actions,
360 size_t n_actions)
064af421 361{
b4b8c781
BP
362 const union ofp_action *a;
363 size_t left;
064af421
BP
364
365 ds_put_cstr(string, "actions=");
b4b8c781 366 if (!n_actions) {
064af421
BP
367 ds_put_cstr(string, "drop");
368 }
38f2e360 369
b4b8c781 370 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
38f2e360
BP
371 int code = ofputil_decode_action(a);
372 if (code >= 0) {
373 if (a != actions) {
374 ds_put_cstr(string, ",");
375 }
376 ofp_print_action(string, a, code);
377 } else {
378 ofp_print_error(string, -code);
064af421 379 }
b4b8c781
BP
380 }
381 if (left > 0) {
382 ds_put_format(string, " ***%zu leftover bytes following actions",
383 left * sizeof *a);
064af421
BP
384 }
385}
386
d1e2cf21
BP
387static void
388ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
389 int verbosity)
064af421 390{
d1e2cf21 391 size_t len = ntohs(opo->header.length);
064af421
BP
392 size_t actions_len = ntohs(opo->actions_len);
393
394 ds_put_cstr(string, " in_port=");
39dc9082 395 ofputil_format_port(ntohs(opo->in_port), string);
064af421
BP
396
397 ds_put_format(string, " actions_len=%zu ", actions_len);
398 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
399 ds_put_format(string, "***packet too short for action length***\n");
400 return;
401 }
b4b8c781
BP
402 if (actions_len % sizeof(union ofp_action)) {
403 ds_put_format(string, "***action length not a multiple of %zu***\n",
404 sizeof(union ofp_action));
405 }
406 ofp_print_actions(string, (const union ofp_action *) opo->actions,
407 actions_len / sizeof(union ofp_action));
064af421
BP
408
409 if (ntohl(opo->buffer_id) == UINT32_MAX) {
410 int data_len = len - sizeof *opo - actions_len;
411 ds_put_format(string, " data_len=%d", data_len);
412 if (verbosity > 0 && len > sizeof *opo) {
413 char *packet = ofp_packet_to_string(
c499c75d 414 (uint8_t *) opo->actions + actions_len, data_len);
064af421
BP
415 ds_put_char(string, '\n');
416 ds_put_cstr(string, packet);
417 free(packet);
418 }
419 } else {
420 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
421 }
422 ds_put_char(string, '\n');
423}
424
425/* qsort comparison function. */
426static int
427compare_ports(const void *a_, const void *b_)
428{
429 const struct ofp_phy_port *a = a_;
430 const struct ofp_phy_port *b = b_;
431 uint16_t ap = ntohs(a->port_no);
432 uint16_t bp = ntohs(b->port_no);
433
434 return ap < bp ? -1 : ap > bp;
435}
436
0ab14c8e
BP
437struct bit_name {
438 uint32_t bit;
439 const char *name;
440};
441
442static void
443ofp_print_bit_names(struct ds *string, uint32_t bits,
444 const struct bit_name bit_names[])
064af421 445{
0ab14c8e
BP
446 int n = 0;
447
448 if (!bits) {
449 ds_put_cstr(string, "0");
064af421
BP
450 return;
451 }
0ab14c8e
BP
452
453 for (; bits && bit_names->name; bit_names++) {
454 if (bits & bit_names->bit) {
455 if (n++) {
456 ds_put_char(string, ' ');
457 }
458 ds_put_cstr(string, bit_names->name);
459 bits &= ~bit_names->bit;
460 }
064af421 461 }
0ab14c8e
BP
462
463 if (bits) {
464 if (n++) {
465 ds_put_char(string, ' ');
466 }
467 ds_put_format(string, "0x%"PRIx32, bits);
064af421 468 }
0ab14c8e
BP
469}
470
471static void
472ofp_print_port_features(struct ds *string, uint32_t features)
473{
474 static const struct bit_name feature_bits[] = {
475 { OFPPF_10MB_HD, "10MB-HD" },
476 { OFPPF_10MB_FD, "10MB-FD" },
477 { OFPPF_100MB_HD, "100MB-HD" },
478 { OFPPF_100MB_FD, "100MB-FD" },
479 { OFPPF_1GB_HD, "1GB-HD" },
480 { OFPPF_1GB_FD, "1GB-FD" },
481 { OFPPF_10GB_FD, "10GB-FD" },
482 { OFPPF_COPPER, "COPPER" },
483 { OFPPF_FIBER, "FIBER" },
484 { OFPPF_AUTONEG, "AUTO_NEG" },
485 { OFPPF_PAUSE, "AUTO_PAUSE" },
486 { OFPPF_PAUSE_ASYM, "AUTO_PAUSE_ASYM" },
487 { 0, NULL },
488 };
489
490 ofp_print_bit_names(string, features, feature_bits);
491 ds_put_char(string, '\n');
492}
493
494static void
495ofp_print_port_config(struct ds *string, uint32_t config)
496{
497 static const struct bit_name config_bits[] = {
498 { OFPPC_PORT_DOWN, "PORT_DOWN" },
499 { OFPPC_NO_STP, "NO_STP" },
500 { OFPPC_NO_RECV, "NO_RECV" },
501 { OFPPC_NO_RECV_STP, "NO_RECV_STP" },
502 { OFPPC_NO_FLOOD, "NO_FLOOD" },
503 { OFPPC_NO_FWD, "NO_FWD" },
504 { OFPPC_NO_PACKET_IN, "NO_PACKET_IN" },
505 { 0, NULL },
506 };
507
508 ofp_print_bit_names(string, config, config_bits);
509 ds_put_char(string, '\n');
510}
511
512static void
513ofp_print_port_state(struct ds *string, uint32_t state)
514{
515 static const struct bit_name state_bits[] = {
516 { OFPPS_LINK_DOWN, "LINK_DOWN" },
517 { 0, NULL },
518 };
519 uint32_t stp_state;
520
521 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
522 * pattern. We have to special case it.
523 *
524 * OVS doesn't support STP, so this field will always be 0 if we are
525 * talking to OVS, so we'd always print STP_LISTEN in that case.
526 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
527 * avoid confusing users. */
528 stp_state = state & OFPPS_STP_MASK;
529 if (stp_state) {
530 ds_put_cstr(string, (stp_state == OFPPS_STP_LEARN ? "STP_LEARN"
531 : stp_state == OFPPS_STP_FORWARD ? "STP_FORWARD"
532 : "STP_BLOCK"));
533 state &= ~OFPPS_STP_MASK;
534 if (state) {
535 ofp_print_bit_names(string, state, state_bits);
536 }
537 } else {
538 ofp_print_bit_names(string, state, state_bits);
064af421
BP
539 }
540 ds_put_char(string, '\n');
541}
542
543static void
544ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
545{
0b61210e 546 char name[OFP_MAX_PORT_NAME_LEN];
064af421
BP
547 int j;
548
549 memcpy(name, port->name, sizeof name);
550 for (j = 0; j < sizeof name - 1; j++) {
858f2852 551 if (!isprint((unsigned char) name[j])) {
064af421
BP
552 break;
553 }
554 }
555 name[j] = '\0';
556
557 ds_put_char(string, ' ');
39dc9082 558 ofputil_format_port(ntohs(port->port_no), string);
0ab14c8e
BP
559 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
560 name, ETH_ADDR_ARGS(port->hw_addr));
561
562 ds_put_cstr(string, " config: ");
563 ofp_print_port_config(string, ntohl(port->config));
564
565 ds_put_cstr(string, " state: ");
566 ofp_print_port_state(string, ntohl(port->state));
567
064af421
BP
568 if (port->curr) {
569 ds_put_format(string, " current: ");
570 ofp_print_port_features(string, ntohl(port->curr));
571 }
572 if (port->advertised) {
573 ds_put_format(string, " advertised: ");
574 ofp_print_port_features(string, ntohl(port->advertised));
575 }
576 if (port->supported) {
577 ds_put_format(string, " supported: ");
578 ofp_print_port_features(string, ntohl(port->supported));
579 }
580 if (port->peer) {
581 ds_put_format(string, " peer: ");
582 ofp_print_port_features(string, ntohl(port->peer));
583 }
584}
585
064af421 586static void
d1e2cf21
BP
587ofp_print_switch_features(struct ds *string,
588 const struct ofp_switch_features *osf)
064af421 589{
d1e2cf21 590 size_t len = ntohs(osf->header.length);
064af421
BP
591 struct ofp_phy_port *port_list;
592 int n_ports;
593 int i;
594
d295e8e9 595 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
064af421
BP
596 osf->header.version, ntohll(osf->datapath_id));
597 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
598 ntohl(osf->n_buffers));
599 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
600 ntohl(osf->capabilities), ntohl(osf->actions));
601
064af421
BP
602 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
603
d295e8e9 604 port_list = xmemdup(osf->ports, len - sizeof *osf);
064af421
BP
605 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
606 for (i = 0; i < n_ports; i++) {
607 ofp_print_phy_port(string, &port_list[i]);
608 }
609 free(port_list);
610}
611
064af421 612static void
d1e2cf21 613ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
064af421 614{
f0fd1a17 615 enum ofp_config_flags flags;
064af421
BP
616
617 flags = ntohs(osc->flags);
3b62feba 618
7257b535
BP
619 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
620 flags &= ~OFPC_FRAG_MASK;
621
f0fd1a17
PS
622 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
623 ds_put_format(string, " invalid_ttl_to_controller");
624 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
625 }
626
064af421
BP
627 if (flags) {
628 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
629 }
630
631 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
632}
633
634static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 635 int verbosity, const char *format, ...)
064af421
BP
636 __attribute__((format(printf, 5, 6)));
637
638static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 639 int verbosity, const char *format, ...)
064af421
BP
640{
641 if (is_wild && verbosity < 2) {
642 return;
643 }
644 ds_put_cstr(string, leader);
645 if (!is_wild) {
646 va_list args;
647
648 va_start(args, format);
649 ds_put_format_valist(string, format, args);
650 va_end(args);
651 } else {
652 ds_put_char(string, '*');
653 }
654 ds_put_char(string, ',');
655}
656
657static void
dbba996b 658print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
064af421
BP
659 uint32_t wild_bits, int verbosity)
660{
661 if (wild_bits >= 32 && verbosity < 2) {
662 return;
663 }
664 ds_put_cstr(string, leader);
665 if (wild_bits < 32) {
666 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
667 if (wild_bits) {
668 ds_put_format(string, "/%d", 32 - wild_bits);
669 }
670 } else {
671 ds_put_char(string, '*');
672 }
673 ds_put_char(string, ',');
674}
675
4f2cad2c 676void
064af421
BP
677ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
678{
679 char *s = ofp_match_to_string(om, verbosity);
680 ds_put_cstr(f, s);
681 free(s);
682}
683
684char *
685ofp_match_to_string(const struct ofp_match *om, int verbosity)
686{
687 struct ds f = DS_EMPTY_INITIALIZER;
688 uint32_t w = ntohl(om->wildcards);
689 bool skip_type = false;
690 bool skip_proto = false;
691
692 if (!(w & OFPFW_DL_TYPE)) {
693 skip_type = true;
694 if (om->dl_type == htons(ETH_TYPE_IP)) {
695 if (!(w & OFPFW_NW_PROTO)) {
696 skip_proto = true;
6767a2cc 697 if (om->nw_proto == IPPROTO_ICMP) {
064af421 698 ds_put_cstr(&f, "icmp,");
6767a2cc 699 } else if (om->nw_proto == IPPROTO_TCP) {
064af421 700 ds_put_cstr(&f, "tcp,");
6767a2cc 701 } else if (om->nw_proto == IPPROTO_UDP) {
064af421
BP
702 ds_put_cstr(&f, "udp,");
703 } else {
704 ds_put_cstr(&f, "ip,");
705 skip_proto = false;
706 }
707 } else {
708 ds_put_cstr(&f, "ip,");
709 }
710 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
711 ds_put_cstr(&f, "arp,");
712 } else {
713 skip_type = false;
714 }
715 }
716 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
717 "%d", ntohs(om->in_port));
718 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
6a1f89c8 719 "%d", ntohs(om->dl_vlan));
959a2ecd
JP
720 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
721 "%d", om->dl_vlan_pcp);
064af421
BP
722 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
723 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
724 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
725 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
726 if (!skip_type) {
727 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
728 "0x%04x", ntohs(om->dl_type));
729 }
730 print_ip_netmask(&f, "nw_src=", om->nw_src,
731 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
732 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
733 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
734 if (!skip_proto) {
fb892732 735 if (om->dl_type == htons(ETH_TYPE_ARP)) {
fb115f91 736 print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
fb892732
JP
737 "%u", om->nw_proto);
738 } else {
739 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
740 "%u", om->nw_proto);
741 }
064af421 742 }
1a960c80
RL
743 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
744 "%u", om->nw_tos);
6767a2cc 745 if (om->nw_proto == IPPROTO_ICMP) {
064af421 746 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
3ee8a9f0 747 "%d", ntohs(om->tp_src));
064af421 748 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
3ee8a9f0 749 "%d", ntohs(om->tp_dst));
064af421
BP
750 } else {
751 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
752 "%d", ntohs(om->tp_src));
753 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
754 "%d", ntohs(om->tp_dst));
755 }
7fa91113
BP
756 if (ds_last(&f) == ',') {
757 f.length--;
758 }
064af421
BP
759 return ds_cstr(&f);
760}
761
064af421 762static void
7fa91113
BP
763ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
764 enum ofputil_msg_code code, int verbosity)
064af421 765{
a9a2da38 766 struct ofputil_flow_mod fm;
f904747b 767 bool need_priority;
90bf1e07 768 enum ofperr error;
064af421 769
00794817 770 error = ofputil_decode_flow_mod(&fm, oh, true);
7fa91113
BP
771 if (error) {
772 ofp_print_error(s, error);
773 return;
3ff4f871
BP
774 }
775
7fa91113
BP
776 ds_put_char(s, ' ');
777 switch (fm.command) {
064af421 778 case OFPFC_ADD:
7fa91113 779 ds_put_cstr(s, "ADD");
064af421
BP
780 break;
781 case OFPFC_MODIFY:
7fa91113 782 ds_put_cstr(s, "MOD");
064af421
BP
783 break;
784 case OFPFC_MODIFY_STRICT:
7fa91113 785 ds_put_cstr(s, "MOD_STRICT");
064af421
BP
786 break;
787 case OFPFC_DELETE:
7fa91113 788 ds_put_cstr(s, "DEL");
064af421
BP
789 break;
790 case OFPFC_DELETE_STRICT:
7fa91113 791 ds_put_cstr(s, "DEL_STRICT");
064af421
BP
792 break;
793 default:
7fa91113
BP
794 ds_put_format(s, "cmd:%d", fm.command);
795 }
6c1491fb 796 if (fm.table_id != 0) {
e896c2d4 797 ds_put_format(s, " table:%d", fm.table_id);
6c1491fb 798 }
7fa91113
BP
799
800 ds_put_char(s, ' ');
801 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
802 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
7fa91113 803 ofp_print_match(s, &ofm->match, verbosity);
f904747b
BP
804
805 /* ofp_print_match() doesn't print priority. */
806 need_priority = true;
7fa91113
BP
807 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
808 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
809 const void *nxm = nfm + 1;
f904747b
BP
810 char *nxm_s;
811
812 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
7fa91113
BP
813 ds_put_cstr(s, nxm_s);
814 free(nxm_s);
f904747b
BP
815
816 /* nx_match_to_string() doesn't print priority. */
817 need_priority = true;
7fa91113
BP
818 } else {
819 cls_rule_format(&fm.cr, s);
f904747b
BP
820
821 /* cls_rule_format() does print priority. */
822 need_priority = false;
3ff4f871 823 }
7fa91113
BP
824
825 if (ds_last(s) != ' ') {
826 ds_put_char(s, ' ');
3ff4f871 827 }
7fa91113
BP
828 if (fm.cookie != htonll(0)) {
829 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
3ff4f871 830 }
7fa91113
BP
831 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
832 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
064af421 833 }
7fa91113
BP
834 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
835 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
3ff4f871 836 }
f904747b 837 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
7fa91113 838 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
3ff4f871 839 }
7fa91113
BP
840 if (fm.buffer_id != UINT32_MAX) {
841 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
3ff4f871 842 }
7fa91113
BP
843 if (fm.flags != 0) {
844 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
845 }
846
b4b8c781 847 ofp_print_actions(s, fm.actions, fm.n_actions);
064af421
BP
848}
849
09862ec6
BP
850static void
851ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
852{
853 ds_put_format(string, "%u", sec);
854 if (nsec > 0) {
855 ds_put_format(string, ".%09u", nsec);
856 while (string->string[string->length - 1] == '0') {
857 string->length--;
858 }
859 }
860 ds_put_char(string, 's');
861}
862
064af421 863static void
9b045a0c 864ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
064af421 865{
9b045a0c 866 struct ofputil_flow_removed fr;
90bf1e07 867 enum ofperr error;
9b045a0c 868
b78f6b77 869 error = ofputil_decode_flow_removed(&fr, oh);
9b045a0c
BP
870 if (error) {
871 ofp_print_error(string, error);
872 return;
873 }
874
fbd76b2e 875 ds_put_char(string, ' ');
9b045a0c
BP
876 cls_rule_format(&fr.rule, string);
877
064af421 878 ds_put_cstr(string, " reason=");
9b045a0c 879 switch (fr.reason) {
ca069229 880 case OFPRR_IDLE_TIMEOUT:
064af421
BP
881 ds_put_cstr(string, "idle");
882 break;
ca069229 883 case OFPRR_HARD_TIMEOUT:
064af421
BP
884 ds_put_cstr(string, "hard");
885 break;
ca069229
JP
886 case OFPRR_DELETE:
887 ds_put_cstr(string, "delete");
888 break;
064af421 889 default:
9b045a0c 890 ds_put_format(string, "**%"PRIu8"**", fr.reason);
064af421
BP
891 break;
892 }
3ff4f871 893
9b045a0c
BP
894 if (fr.cookie != htonll(0)) {
895 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
3ff4f871 896 }
09862ec6 897 ds_put_cstr(string, " duration");
9b045a0c 898 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
09862ec6 899 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
9b045a0c 900 fr.idle_timeout, fr.packet_count, fr.byte_count);
064af421
BP
901}
902
903static void
d1e2cf21 904ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
064af421 905{
064af421 906 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
d295e8e9 907 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
064af421
BP
908 ntohl(opm->config), ntohl(opm->mask));
909 ds_put_format(string, " advertise: ");
910 if (opm->advertise) {
911 ofp_print_port_features(string, ntohl(opm->advertise));
912 } else {
913 ds_put_format(string, "UNCHANGED\n");
914 }
915}
916
7fa91113 917static void
90bf1e07 918ofp_print_error(struct ds *string, enum ofperr error)
7fa91113 919{
7fa91113
BP
920 if (string->length) {
921 ds_put_char(string, ' ');
922 }
90bf1e07 923 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
f74be05a
JP
924}
925
064af421 926static void
d1e2cf21 927ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
064af421 928{
d1e2cf21 929 size_t len = ntohs(oem->header.length);
dc4762ed
BP
930 size_t payload_ofs, payload_len;
931 const void *payload;
90bf1e07 932 enum ofperr error;
064af421
BP
933 char *s;
934
90bf1e07
BP
935 error = ofperr_decode_msg(&oem->header, &payload_ofs);
936 if (!error) {
937 ds_put_cstr(string, "***decode error***");
dc4762ed
BP
938 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
939 return;
f74be05a
JP
940 }
941
90bf1e07 942 ds_put_format(string, " %s\n", ofperr_get_name(error));
064af421 943
dc4762ed
BP
944 payload = (const uint8_t *) oem + payload_ofs;
945 payload_len = len - payload_ofs;
90bf1e07 946 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
dc4762ed 947 ds_put_printable(string, payload, payload_len);
90bf1e07 948 } else {
dc4762ed 949 s = ofp_to_string(payload, payload_len, 1);
064af421
BP
950 ds_put_cstr(string, s);
951 free(s);
064af421
BP
952 }
953}
954
064af421 955static void
d1e2cf21 956ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
064af421 957{
064af421
BP
958 if (ops->reason == OFPPR_ADD) {
959 ds_put_format(string, " ADD:");
960 } else if (ops->reason == OFPPR_DELETE) {
961 ds_put_format(string, " DEL:");
962 } else if (ops->reason == OFPPR_MODIFY) {
963 ds_put_format(string, " MOD:");
964 }
965
966 ofp_print_phy_port(string, &ops->desc);
967}
968
969static void
63f2140a 970ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
064af421 971{
fbd76b2e 972 ds_put_char(string, '\n');
d295e8e9 973 ds_put_format(string, "Manufacturer: %.*s\n",
dd70b475
JP
974 (int) sizeof ods->mfr_desc, ods->mfr_desc);
975 ds_put_format(string, "Hardware: %.*s\n",
976 (int) sizeof ods->hw_desc, ods->hw_desc);
977 ds_put_format(string, "Software: %.*s\n",
978 (int) sizeof ods->sw_desc, ods->sw_desc);
979 ds_put_format(string, "Serial Num: %.*s\n",
980 (int) sizeof ods->serial_num, ods->serial_num);
981 ds_put_format(string, "DP Description: %.*s\n",
982 (int) sizeof ods->dp_desc, ods->dp_desc);
064af421
BP
983}
984
985static void
76c93b22
BP
986ofp_print_flow_stats_request(struct ds *string,
987 const struct ofp_stats_msg *osm)
064af421 988{
81d1ea94 989 struct ofputil_flow_stats_request fsr;
90bf1e07 990 enum ofperr error;
064af421 991
76c93b22 992 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
2af0c0ef
BP
993 if (error) {
994 ofp_print_error(string, error);
995 return;
996 }
997
998 if (fsr.table_id != 0xff) {
e896c2d4 999 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
064af421
BP
1000 }
1001
2af0c0ef
BP
1002 if (fsr.out_port != OFPP_NONE) {
1003 ds_put_cstr(string, " out_port=");
39dc9082 1004 ofputil_format_port(fsr.out_port, string);
2af0c0ef
BP
1005 }
1006
54ae6fa8
BP
1007 /* A flow stats request doesn't include a priority, but cls_rule_format()
1008 * will print one unless it is OFP_DEFAULT_PRIORITY. */
1009 fsr.match.priority = OFP_DEFAULT_PRIORITY;
1010
2af0c0ef
BP
1011 ds_put_char(string, ' ');
1012 cls_rule_format(&fsr.match, string);
064af421
BP
1013}
1014
1015static void
4ffd1b43 1016ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
064af421 1017{
4ffd1b43 1018 struct ofpbuf b;
064af421 1019
4ffd1b43
BP
1020 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1021 for (;;) {
1022 struct ofputil_flow_stats fs;
1023 int retval;
fab8fadb 1024
b78f6b77 1025 retval = ofputil_decode_flow_stats_reply(&fs, &b);
4ffd1b43
BP
1026 if (retval) {
1027 if (retval != EOF) {
1028 ds_put_cstr(string, " ***parse error***");
064af421
BP
1029 }
1030 break;
1031 }
1032
8961de6a
BP
1033 ds_put_char(string, '\n');
1034
09862ec6 1035 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
4ffd1b43
BP
1036 ntohll(fs.cookie));
1037 ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
e896c2d4 1038 ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
4ffd1b43
BP
1039 ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
1040 ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
1041 if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
1042 ds_put_format(string, "idle_timeout=%"PRIu16",", fs.idle_timeout);
c6430da5 1043 }
4ffd1b43
BP
1044 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1045 ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout);
c6430da5
BP
1046 }
1047
4ffd1b43 1048 cls_rule_format(&fs.rule, string);
05b8f1c2
BP
1049 if (string->string[string->length - 1] != ' ') {
1050 ds_put_char(string, ' ');
1051 }
b4b8c781 1052 ofp_print_actions(string, fs.actions, fs.n_actions);
c6430da5
BP
1053 }
1054}
1055
064af421 1056static void
63f2140a
BP
1057ofp_print_ofpst_aggregate_reply(struct ds *string,
1058 const struct ofp_aggregate_stats_reply *asr)
064af421 1059{
c4617b3c
BP
1060 ds_put_format(string, " packet_count=%"PRIu64,
1061 ntohll(get_32aligned_be64(&asr->packet_count)));
1062 ds_put_format(string, " byte_count=%"PRIu64,
1063 ntohll(get_32aligned_be64(&asr->byte_count)));
064af421
BP
1064 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1065}
1066
a2ad9ecd
BP
1067static void
1068ofp_print_nxst_aggregate_reply(struct ds *string,
1069 const struct nx_aggregate_stats_reply *nasr)
1070{
675baf0c
BP
1071 ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1072 ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1073 ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
a2ad9ecd
BP
1074}
1075
d295e8e9 1076static void print_port_stat(struct ds *string, const char *leader,
c4617b3c 1077 const ovs_32aligned_be64 *statp, int more)
064af421 1078{
c4617b3c
BP
1079 uint64_t stat = ntohll(get_32aligned_be64(statp));
1080
064af421 1081 ds_put_cstr(string, leader);
c4617b3c 1082 if (stat != UINT64_MAX) {
064af421
BP
1083 ds_put_format(string, "%"PRIu64, stat);
1084 } else {
1085 ds_put_char(string, '?');
1086 }
1087 if (more) {
1088 ds_put_cstr(string, ", ");
1089 } else {
1090 ds_put_cstr(string, "\n");
1091 }
1092}
1093
abaad8cf 1094static void
63f2140a
BP
1095ofp_print_ofpst_port_request(struct ds *string,
1096 const struct ofp_port_stats_request *psr)
abaad8cf 1097{
fbd76b2e 1098 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
abaad8cf
JP
1099}
1100
064af421 1101static void
d1e2cf21
BP
1102ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1103 int verbosity)
064af421 1104{
d1e2cf21
BP
1105 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1106 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
064af421
BP
1107 ds_put_format(string, " %zu ports\n", n);
1108 if (verbosity < 1) {
1109 return;
1110 }
1111
1112 for (; n--; ps++) {
1113 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1114
1115 ds_put_cstr(string, "rx ");
c4617b3c
BP
1116 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1117 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1118 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1119 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1120 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1121 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1122 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
064af421
BP
1123
1124 ds_put_cstr(string, " tx ");
c4617b3c
BP
1125 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1126 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1127 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1128 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1129 print_port_stat(string, "coll=", &ps->collisions, 0);
064af421
BP
1130 }
1131}
1132
1133static void
d1e2cf21
BP
1134ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1135 int verbosity)
064af421 1136{
d1e2cf21
BP
1137 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1138 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
064af421
BP
1139 ds_put_format(string, " %zu tables\n", n);
1140 if (verbosity < 1) {
1141 return;
1142 }
1143
1144 for (; n--; ts++) {
1145 char name[OFP_MAX_TABLE_NAME_LEN + 1];
e868fb3d 1146 ovs_strlcpy(name, ts->name, sizeof name);
064af421
BP
1147
1148 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1149 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1150 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1151 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1152 ds_put_cstr(string, " ");
d295e8e9 1153 ds_put_format(string, "lookup=%"PRIu64", ",
c4617b3c 1154 ntohll(get_32aligned_be64(&ts->lookup_count)));
064af421 1155 ds_put_format(string, "matched=%"PRIu64"\n",
c4617b3c 1156 ntohll(get_32aligned_be64(&ts->matched_count)));
064af421
BP
1157 }
1158}
1159
d2805da2
BP
1160static void
1161ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1162{
1163 if (queue_id == OFPQ_ALL) {
1164 ds_put_cstr(string, "ALL");
1165 } else {
1166 ds_put_format(string, "%"PRIu32, queue_id);
1167 }
1168}
1169
1170static void
63f2140a
BP
1171ofp_print_ofpst_queue_request(struct ds *string,
1172 const struct ofp_queue_stats_request *qsr)
d2805da2 1173{
d2805da2 1174 ds_put_cstr(string, "port=");
39dc9082 1175 ofputil_format_port(ntohs(qsr->port_no), string);
d2805da2
BP
1176
1177 ds_put_cstr(string, " queue=");
1178 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1179}
1180
1181static void
d1e2cf21
BP
1182ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1183 int verbosity)
d2805da2 1184{
d1e2cf21
BP
1185 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1186 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
d2805da2
BP
1187 ds_put_format(string, " %zu queues\n", n);
1188 if (verbosity < 1) {
1189 return;
1190 }
1191
1192 for (; n--; qs++) {
1193 ds_put_cstr(string, " port ");
39dc9082 1194 ofputil_format_port(ntohs(qs->port_no), string);
d2805da2
BP
1195 ds_put_cstr(string, " queue ");
1196 ofp_print_queue_name(string, ntohl(qs->queue_id));
1197 ds_put_cstr(string, ": ");
1198
c4617b3c
BP
1199 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1200 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1201 print_port_stat(string, "errors=", &qs->tx_errors, 0);
d2805da2
BP
1202 }
1203}
1204
064af421 1205static void
d1e2cf21 1206ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
064af421 1207{
28c8bad1 1208 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
064af421
BP
1209
1210 if (srq->flags) {
1211 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1212 ntohs(srq->flags));
1213 }
064af421
BP
1214}
1215
1216static void
d1e2cf21 1217ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
064af421 1218{
28c8bad1 1219 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
064af421 1220
d1e2cf21 1221 if (srp->flags) {
064af421 1222 uint16_t flags = ntohs(srp->flags);
d1e2cf21
BP
1223
1224 ds_put_cstr(string, " flags=");
064af421
BP
1225 if (flags & OFPSF_REPLY_MORE) {
1226 ds_put_cstr(string, "[more]");
1227 flags &= ~OFPSF_REPLY_MORE;
1228 }
1229 if (flags) {
d1e2cf21
BP
1230 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1231 flags);
064af421
BP
1232 }
1233 }
064af421
BP
1234}
1235
1236static void
d1e2cf21 1237ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
064af421 1238{
d1e2cf21 1239 size_t len = ntohs(oh->length);
064af421 1240
d1e2cf21 1241 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
064af421 1242 if (verbosity > 1) {
d1e2cf21 1243 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
064af421
BP
1244 }
1245}
1246
61fe3a7b
BP
1247static void
1248ofp_print_nxt_role_message(struct ds *string,
1249 const struct nx_role_request *nrr)
1250{
1251 unsigned int role = ntohl(nrr->role);
1252
1253 ds_put_cstr(string, " role=");
1254 if (role == NX_ROLE_OTHER) {
1255 ds_put_cstr(string, "other");
1256 } else if (role == NX_ROLE_MASTER) {
1257 ds_put_cstr(string, "master");
1258 } else if (role == NX_ROLE_SLAVE) {
1259 ds_put_cstr(string, "slave");
1260 } else {
1261 ds_put_format(string, "%u", role);
1262 }
1263}
1264
6c1491fb
BP
1265static void
1266ofp_print_nxt_flow_mod_table_id(struct ds *string,
73dbf4ab 1267 const struct nx_flow_mod_table_id *nfmti)
6c1491fb
BP
1268{
1269 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1270}
1271
7fa91113
BP
1272static void
1273ofp_print_nxt_set_flow_format(struct ds *string,
73dbf4ab 1274 const struct nx_set_flow_format *nsff)
7fa91113
BP
1275{
1276 uint32_t format = ntohl(nsff->format);
1277
1278 ds_put_cstr(string, " format=");
1279 if (ofputil_flow_format_is_valid(format)) {
1280 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1281 } else {
1282 ds_put_format(string, "%"PRIu32, format);
1283 }
1284}
1285
54834960
EJ
1286static void
1287ofp_print_nxt_set_packet_in_format(struct ds *string,
73dbf4ab 1288 const struct nx_set_packet_in_format *nspf)
54834960
EJ
1289{
1290 uint32_t format = ntohl(nspf->format);
1291
1292 ds_put_cstr(string, " format=");
1293 if (ofputil_packet_in_format_is_valid(format)) {
1294 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1295 } else {
1296 ds_put_format(string, "%"PRIu32, format);
1297 }
1298}
1299
d1e2cf21
BP
1300static void
1301ofp_to_string__(const struct ofp_header *oh,
1302 const struct ofputil_msg_type *type, struct ds *string,
1303 int verbosity)
1304{
7fa91113 1305 enum ofputil_msg_code code;
d1e2cf21
BP
1306 const void *msg = oh;
1307
1308 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1309 ofputil_msg_type_name(type), ntohl(oh->xid));
1310
7fa91113
BP
1311 code = ofputil_msg_type_code(type);
1312 switch (code) {
8f93e93c 1313 case OFPUTIL_MSG_INVALID:
d1e2cf21
BP
1314 break;
1315
1316 case OFPUTIL_OFPT_HELLO:
dc4762ed
BP
1317 ds_put_char(string, '\n');
1318 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1319 0, true);
d1e2cf21
BP
1320 break;
1321
1322 case OFPUTIL_OFPT_ERROR:
1323 ofp_print_error_msg(string, msg);
1324 break;
1325
1326 case OFPUTIL_OFPT_ECHO_REQUEST:
1327 case OFPUTIL_OFPT_ECHO_REPLY:
1328 ofp_print_echo(string, oh, verbosity);
1329 break;
1330
1331 case OFPUTIL_OFPT_FEATURES_REQUEST:
1332 break;
1333
1334 case OFPUTIL_OFPT_FEATURES_REPLY:
1335 ofp_print_switch_features(string, msg);
1336 break;
1337
1338 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1339 break;
1340
1341 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1342 case OFPUTIL_OFPT_SET_CONFIG:
1343 ofp_print_switch_config(string, msg);
1344 break;
1345
1346 case OFPUTIL_OFPT_PACKET_IN:
54834960 1347 case OFPUTIL_NXT_PACKET_IN:
d1e2cf21
BP
1348 ofp_print_packet_in(string, msg, verbosity);
1349 break;
1350
1351 case OFPUTIL_OFPT_FLOW_REMOVED:
9b045a0c
BP
1352 case OFPUTIL_NXT_FLOW_REMOVED:
1353 ofp_print_flow_removed(string, msg);
d1e2cf21
BP
1354 break;
1355
1356 case OFPUTIL_OFPT_PORT_STATUS:
1357 ofp_print_port_status(string, msg);
1358 break;
1359
1360 case OFPUTIL_OFPT_PACKET_OUT:
1361 ofp_print_packet_out(string, msg, verbosity);
1362 break;
1363
1364 case OFPUTIL_OFPT_FLOW_MOD:
7fa91113 1365 ofp_print_flow_mod(string, msg, code, verbosity);
d1e2cf21
BP
1366 break;
1367
1368 case OFPUTIL_OFPT_PORT_MOD:
1369 ofp_print_port_mod(string, msg);
1370 break;
1371
1372 case OFPUTIL_OFPT_BARRIER_REQUEST:
1373 case OFPUTIL_OFPT_BARRIER_REPLY:
1374 break;
1375
1376 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1377 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1378 /* XXX */
1379 break;
1380
1381 case OFPUTIL_OFPST_DESC_REQUEST:
1382 ofp_print_stats_request(string, oh);
1383 break;
1384
1385 case OFPUTIL_OFPST_FLOW_REQUEST:
2af0c0ef 1386 case OFPUTIL_NXST_FLOW_REQUEST:
d1e2cf21 1387 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
2af0c0ef 1388 case OFPUTIL_NXST_AGGREGATE_REQUEST:
d1e2cf21 1389 ofp_print_stats_request(string, oh);
76c93b22 1390 ofp_print_flow_stats_request(string, msg);
d1e2cf21
BP
1391 break;
1392
1393 case OFPUTIL_OFPST_TABLE_REQUEST:
1394 ofp_print_stats_request(string, oh);
1395 break;
1396
1397 case OFPUTIL_OFPST_PORT_REQUEST:
1398 ofp_print_stats_request(string, oh);
63f2140a 1399 ofp_print_ofpst_port_request(string, msg);
d1e2cf21
BP
1400 break;
1401
1402 case OFPUTIL_OFPST_QUEUE_REQUEST:
1403 ofp_print_stats_request(string, oh);
63f2140a 1404 ofp_print_ofpst_queue_request(string, msg);
d1e2cf21
BP
1405 break;
1406
1407 case OFPUTIL_OFPST_DESC_REPLY:
1408 ofp_print_stats_reply(string, oh);
63f2140a 1409 ofp_print_ofpst_desc_reply(string, msg);
d1e2cf21
BP
1410 break;
1411
1412 case OFPUTIL_OFPST_FLOW_REPLY:
4ffd1b43 1413 case OFPUTIL_NXST_FLOW_REPLY:
d1e2cf21 1414 ofp_print_stats_reply(string, oh);
4ffd1b43 1415 ofp_print_flow_stats_reply(string, oh);
d1e2cf21
BP
1416 break;
1417
1418 case OFPUTIL_OFPST_QUEUE_REPLY:
1419 ofp_print_stats_reply(string, oh);
1420 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1421 break;
1422
1423 case OFPUTIL_OFPST_PORT_REPLY:
1424 ofp_print_stats_reply(string, oh);
1425 ofp_print_ofpst_port_reply(string, oh, verbosity);
1426 break;
1427
1428 case OFPUTIL_OFPST_TABLE_REPLY:
1429 ofp_print_stats_reply(string, oh);
1430 ofp_print_ofpst_table_reply(string, oh, verbosity);
1431 break;
1432
1433 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1434 ofp_print_stats_reply(string, oh);
63f2140a 1435 ofp_print_ofpst_aggregate_reply(string, msg);
d1e2cf21 1436 break;
064af421 1437
d1e2cf21
BP
1438 case OFPUTIL_NXT_ROLE_REQUEST:
1439 case OFPUTIL_NXT_ROLE_REPLY:
61fe3a7b 1440 ofp_print_nxt_role_message(string, msg);
7fa91113
BP
1441 break;
1442
6c1491fb
BP
1443 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1444 ofp_print_nxt_flow_mod_table_id(string, msg);
1445 break;
1446
d1e2cf21 1447 case OFPUTIL_NXT_SET_FLOW_FORMAT:
7fa91113
BP
1448 ofp_print_nxt_set_flow_format(string, msg);
1449 break;
1450
54834960
EJ
1451 case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
1452 ofp_print_nxt_set_packet_in_format(string, msg);
1453 break;
1454
d1e2cf21 1455 case OFPUTIL_NXT_FLOW_MOD:
7fa91113
BP
1456 ofp_print_flow_mod(string, msg, code, verbosity);
1457 break;
1458
d1e2cf21 1459 case OFPUTIL_NXST_AGGREGATE_REPLY:
a2ad9ecd 1460 ofp_print_stats_reply(string, oh);
9b045a0c 1461 ofp_print_nxst_aggregate_reply(string, msg);
d1e2cf21 1462 break;
246e61ea 1463 }
d1e2cf21 1464}
064af421
BP
1465
1466/* Composes and returns a string representing the OpenFlow packet of 'len'
1467 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1468 * verbosity and higher numbers increase verbosity. The caller is responsible
1469 * for freeing the string. */
1470char *
1471ofp_to_string(const void *oh_, size_t len, int verbosity)
1472{
1473 struct ds string = DS_EMPTY_INITIALIZER;
1474 const struct ofp_header *oh = oh_;
064af421 1475
49ad0403
BP
1476 if (!len) {
1477 ds_put_cstr(&string, "OpenFlow message is empty\n");
1478 } else if (len < sizeof(struct ofp_header)) {
1479 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1480 len);
d1e2cf21
BP
1481 } else if (ntohs(oh->length) > len) {
1482 ds_put_format(&string,
49ad0403 1483 "(***truncated to %zu bytes from %"PRIu16"***)\n",
d1e2cf21
BP
1484 len, ntohs(oh->length));
1485 } else if (ntohs(oh->length) < len) {
1486 ds_put_format(&string,
1487 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1488 ntohs(oh->length), len);
1489 } else {
1490 const struct ofputil_msg_type *type;
90bf1e07 1491 enum ofperr error;
d1e2cf21
BP
1492
1493 error = ofputil_decode_msg_type(oh, &type);
1494 if (!error) {
1495 ofp_to_string__(oh, type, &string, verbosity);
7fa91113
BP
1496 if (verbosity >= 5) {
1497 if (ds_last(&string) != '\n') {
1498 ds_put_char(&string, '\n');
1499 }
d1e2cf21
BP
1500 ds_put_hex_dump(&string, oh, len, 0, true);
1501 }
7fa91113 1502 if (ds_last(&string) != '\n') {
d1e2cf21
BP
1503 ds_put_char(&string, '\n');
1504 }
1505 return ds_steal_cstr(&string);
064af421 1506 }
064af421 1507
7fa91113 1508 ofp_print_error(&string, error);
064af421 1509 }
d1e2cf21
BP
1510 ds_put_hex_dump(&string, oh, len, 0, true);
1511 return ds_steal_cstr(&string);
064af421
BP
1512}
1513
1514/* Returns the name for the specified OpenFlow message type as a string,
1515 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1516 * hex number, e.g. "0x55".
1517 *
1518 * The caller must free the returned string when it is no longer needed. */
1519char *
1520ofp_message_type_to_string(uint8_t type)
1521{
d1e2cf21 1522 const char *name;
064af421 1523
d1e2cf21
BP
1524 switch (type) {
1525 case OFPT_HELLO:
1526 name = "HELLO";
1527 break;
1528 case OFPT_ERROR:
1529 name = "ERROR";
1530 break;
1531 case OFPT_ECHO_REQUEST:
1532 name = "ECHO_REQUEST";
1533 break;
1534 case OFPT_ECHO_REPLY:
1535 name = "ECHO_REPLY";
1536 break;
1537 case OFPT_VENDOR:
1538 name = "VENDOR";
1539 break;
1540 case OFPT_FEATURES_REQUEST:
1541 name = "FEATURES_REQUEST";
1542 break;
1543 case OFPT_FEATURES_REPLY:
1544 name = "FEATURES_REPLY";
1545 break;
1546 case OFPT_GET_CONFIG_REQUEST:
1547 name = "GET_CONFIG_REQUEST";
1548 break;
1549 case OFPT_GET_CONFIG_REPLY:
1550 name = "GET_CONFIG_REPLY";
1551 break;
1552 case OFPT_SET_CONFIG:
1553 name = "SET_CONFIG";
1554 break;
1555 case OFPT_PACKET_IN:
1556 name = "PACKET_IN";
1557 break;
1558 case OFPT_FLOW_REMOVED:
1559 name = "FLOW_REMOVED";
1560 break;
1561 case OFPT_PORT_STATUS:
1562 name = "PORT_STATUS";
1563 break;
1564 case OFPT_PACKET_OUT:
1565 name = "PACKET_OUT";
1566 break;
1567 case OFPT_FLOW_MOD:
1568 name = "FLOW_MOD";
1569 break;
1570 case OFPT_PORT_MOD:
1571 name = "PORT_MOD";
1572 break;
1573 case OFPT_STATS_REQUEST:
1574 name = "STATS_REQUEST";
1575 break;
1576 case OFPT_STATS_REPLY:
1577 name = "STATS_REPLY";
1578 break;
1579 case OFPT_BARRIER_REQUEST:
1580 name = "BARRIER_REQUEST";
1581 break;
1582 case OFPT_BARRIER_REPLY:
1583 name = "BARRIER_REPLY";
1584 break;
1585 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1586 name = "QUEUE_GET_CONFIG_REQUEST";
1587 break;
1588 case OFPT_QUEUE_GET_CONFIG_REPLY:
1589 name = "QUEUE_GET_CONFIG_REPLY";
1590 break;
1591 default:
1592 name = NULL;
1593 break;
064af421 1594 }
d1e2cf21
BP
1595
1596 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
064af421
BP
1597}
1598\f
1599static void
d295e8e9 1600print_and_free(FILE *stream, char *string)
064af421
BP
1601{
1602 fputs(string, stream);
1603 free(string);
1604}
1605
1606/* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1607 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1608 * numbers increase verbosity. */
1609void
1610ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1611{
1612 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1613}
1614
1615/* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
897a8e07 1616 * 'data' to 'stream'. */
064af421 1617void
c499c75d 1618ofp_print_packet(FILE *stream, const void *data, size_t len)
064af421 1619{
c499c75d 1620 print_and_free(stream, ofp_packet_to_string(data, len));
064af421 1621}