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