]> git.proxmox.com Git - ovs.git/blame - lib/ofp-print.c
nicira-ext: Remove unused macro NICIRA_OUI_STR.
[ovs.git] / lib / ofp-print.c
CommitLineData
064af421 1/*
67a4917b 2 * Copyright (c) 2008, 2009, 2010 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
10a24935 29#include "byte-order.h"
064af421
BP
30#include "compiler.h"
31#include "dynamic-string.h"
32#include "flow.h"
7fa91113 33#include "nx-match.h"
69b6be19 34#include "ofp-util.h"
064af421
BP
35#include "ofpbuf.h"
36#include "openflow/openflow.h"
37#include "openflow/nicira-ext.h"
38#include "packets.h"
39#include "pcap.h"
40#include "util.h"
41
42static void ofp_print_port_name(struct ds *string, uint16_t port);
d2805da2 43static void ofp_print_queue_name(struct ds *string, uint32_t port);
7fa91113
BP
44static void ofp_print_error(struct ds *, int error);
45
064af421
BP
46
47/* Returns a string that represents the contents of the Ethernet frame in the
48 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
49 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
50 * bytes were captured).
51 *
52 * The caller must free the returned string.
53 *
54 * This starts and kills a tcpdump subprocess so it's quite expensive. */
55char *
67a4917b 56ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
064af421
BP
57{
58 struct ds ds = DS_EMPTY_INITIALIZER;
59 struct ofpbuf buf;
60
61 char command[128];
62 FILE *pcap;
63 FILE *tcpdump;
64 int status;
65 int c;
66
0bc9407d 67 ofpbuf_use_const(&buf, data, len);
064af421
BP
68
69 pcap = tmpfile();
70 if (!pcap) {
71 ovs_error(errno, "tmpfile");
72 return xstrdup("<error>");
73 }
74 pcap_write_header(pcap);
75 pcap_write(pcap, &buf);
76 fflush(pcap);
77 if (ferror(pcap)) {
78 ovs_error(errno, "error writing temporary file");
79 }
80 rewind(pcap);
81
82 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
83 fileno(pcap));
84 tcpdump = popen(command, "r");
85 fclose(pcap);
86 if (!tcpdump) {
87 ovs_error(errno, "exec(\"%s\")", command);
88 return xstrdup("<error>");
89 }
90
91 while ((c = getc(tcpdump)) != EOF) {
92 ds_put_char(&ds, c);
93 }
94
95 status = pclose(tcpdump);
96 if (WIFEXITED(status)) {
97 if (WEXITSTATUS(status))
98 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
99 } else if (WIFSIGNALED(status)) {
d295e8e9 100 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
064af421
BP
101 }
102 return ds_cstr(&ds);
103}
104
064af421 105static void
d1e2cf21
BP
106ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
107 int verbosity)
064af421 108{
d1e2cf21 109 size_t len = ntohs(op->header.length);
064af421
BP
110 size_t data_len;
111
112 ds_put_format(string, " total_len=%"PRIu16" in_port=",
113 ntohs(op->total_len));
114 ofp_print_port_name(string, ntohs(op->in_port));
115
116 if (op->reason == OFPR_ACTION)
117 ds_put_cstr(string, " (via action)");
118 else if (op->reason != OFPR_NO_MATCH)
119 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
120
121 data_len = len - offsetof(struct ofp_packet_in, data);
122 ds_put_format(string, " data_len=%zu", data_len);
123 if (htonl(op->buffer_id) == UINT32_MAX) {
124 ds_put_format(string, " (unbuffered)");
125 if (ntohs(op->total_len) != data_len)
126 ds_put_format(string, " (***total_len != data_len***)");
127 } else {
128 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
129 if (ntohs(op->total_len) < data_len)
130 ds_put_format(string, " (***total_len < data_len***)");
131 }
132 ds_put_char(string, '\n');
133
134 if (verbosity > 0) {
ae412e7d 135 struct flow flow;
064af421 136 struct ofpbuf packet;
8321fb9c 137
0bc9407d 138 ofpbuf_use_const(&packet, op->data, data_len);
659586ef 139 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
8321fb9c 140 flow_format(string, &flow);
064af421
BP
141 ds_put_char(string, '\n');
142 }
143 if (verbosity > 1) {
144 char *packet = ofp_packet_to_string(op->data, data_len,
d295e8e9 145 ntohs(op->total_len));
064af421
BP
146 ds_put_cstr(string, packet);
147 free(packet);
148 }
149}
150
d295e8e9 151static void ofp_print_port_name(struct ds *string, uint16_t port)
064af421
BP
152{
153 const char *name;
154 switch (port) {
155 case OFPP_IN_PORT:
156 name = "IN_PORT";
157 break;
158 case OFPP_TABLE:
159 name = "TABLE";
160 break;
161 case OFPP_NORMAL:
162 name = "NORMAL";
163 break;
164 case OFPP_FLOOD:
165 name = "FLOOD";
166 break;
167 case OFPP_ALL:
168 name = "ALL";
169 break;
170 case OFPP_CONTROLLER:
171 name = "CONTROLLER";
172 break;
173 case OFPP_LOCAL:
174 name = "LOCAL";
175 break;
176 case OFPP_NONE:
177 name = "NONE";
178 break;
179 default:
180 ds_put_format(string, "%"PRIu16, port);
181 return;
182 }
183 ds_put_cstr(string, name);
184}
185
96fc46e8
BP
186static void
187print_note(struct ds *string, const struct nx_action_note *nan)
188{
189 size_t len;
190 size_t i;
191
192 ds_put_cstr(string, "note:");
193 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
194 for (i = 0; i < len; i++) {
195 if (i) {
196 ds_put_char(string, '.');
197 }
198 ds_put_format(string, "%02"PRIx8, nan->note[i]);
199 }
200}
201
064af421
BP
202static void
203ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
204{
205 switch (ntohs(nah->subtype)) {
206 case NXAST_RESUBMIT: {
207 const struct nx_action_resubmit *nar = (struct nx_action_resubmit *)nah;
208 ds_put_format(string, "resubmit:");
209 ofp_print_port_name(string, ntohs(nar->in_port));
210 break;
211 }
212
659586ef
JG
213 case NXAST_SET_TUNNEL: {
214 const struct nx_action_set_tunnel *nast =
215 (struct nx_action_set_tunnel *)nah;
5b16f009 216 ds_put_format(string, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
659586ef
JG
217 break;
218 }
219
a77d89b8
BP
220 case NXAST_DROP_SPOOFED_ARP:
221 ds_put_cstr(string, "drop_spoofed_arp");
222 break;
223
eedc0097
JP
224 case NXAST_SET_QUEUE: {
225 const struct nx_action_set_queue *nasq =
226 (struct nx_action_set_queue *)nah;
227 ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
228 break;
229 }
230
231 case NXAST_POP_QUEUE:
232 ds_put_cstr(string, "pop_queue");
233 break;
234
96fc46e8
BP
235 case NXAST_NOTE:
236 print_note(string, (const struct nx_action_note *) nah);
237 break;
238
064af421 239 default:
a77d89b8 240 ds_put_format(string, "***unknown Nicira action:%d***",
064af421
BP
241 ntohs(nah->subtype));
242 }
243}
244
245static int
d295e8e9
JP
246ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
247 size_t actions_len)
064af421
BP
248{
249 uint16_t type;
250 size_t len;
251
252 struct openflow_action {
253 size_t min_size;
254 size_t max_size;
255 };
256
257 const struct openflow_action of_actions[] = {
258 [OFPAT_OUTPUT] = {
259 sizeof(struct ofp_action_output),
260 sizeof(struct ofp_action_output),
261 },
262 [OFPAT_SET_VLAN_VID] = {
263 sizeof(struct ofp_action_vlan_vid),
264 sizeof(struct ofp_action_vlan_vid),
265 },
266 [OFPAT_SET_VLAN_PCP] = {
267 sizeof(struct ofp_action_vlan_pcp),
268 sizeof(struct ofp_action_vlan_pcp),
269 },
270 [OFPAT_STRIP_VLAN] = {
271 sizeof(struct ofp_action_header),
272 sizeof(struct ofp_action_header),
273 },
274 [OFPAT_SET_DL_SRC] = {
275 sizeof(struct ofp_action_dl_addr),
276 sizeof(struct ofp_action_dl_addr),
277 },
278 [OFPAT_SET_DL_DST] = {
279 sizeof(struct ofp_action_dl_addr),
280 sizeof(struct ofp_action_dl_addr),
281 },
282 [OFPAT_SET_NW_SRC] = {
283 sizeof(struct ofp_action_nw_addr),
284 sizeof(struct ofp_action_nw_addr),
285 },
286 [OFPAT_SET_NW_DST] = {
287 sizeof(struct ofp_action_nw_addr),
288 sizeof(struct ofp_action_nw_addr),
289 },
959a2ecd
JP
290 [OFPAT_SET_NW_TOS] = {
291 sizeof(struct ofp_action_nw_tos),
292 sizeof(struct ofp_action_nw_tos),
293 },
064af421
BP
294 [OFPAT_SET_TP_SRC] = {
295 sizeof(struct ofp_action_tp_port),
296 sizeof(struct ofp_action_tp_port),
297 },
298 [OFPAT_SET_TP_DST] = {
299 sizeof(struct ofp_action_tp_port),
300 sizeof(struct ofp_action_tp_port),
dab45bfd
BP
301 },
302 [OFPAT_ENQUEUE] = {
303 sizeof(struct ofp_action_enqueue),
304 sizeof(struct ofp_action_enqueue),
064af421
BP
305 }
306 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
307 };
308
309 if (actions_len < sizeof *ah) {
310 ds_put_format(string, "***action array too short for next action***\n");
311 return -1;
312 }
313
314 type = ntohs(ah->type);
315 len = ntohs(ah->len);
316 if (actions_len < len) {
317 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
318 return -1;
319 }
320
1d87ef5c
BP
321 if (!len) {
322 ds_put_format(string, "***zero-length action***\n");
323 return 8;
324 }
325
69b6be19 326 if ((len % OFP_ACTION_ALIGN) != 0) {
d295e8e9 327 ds_put_format(string,
69b6be19
BP
328 "***action %"PRIu16" length not a multiple of %d***\n",
329 type, OFP_ACTION_ALIGN);
064af421
BP
330 return -1;
331 }
332
333 if (type < ARRAY_SIZE(of_actions)) {
334 const struct openflow_action *act = &of_actions[type];
335 if ((len < act->min_size) || (len > act->max_size)) {
d295e8e9 336 ds_put_format(string,
064af421
BP
337 "***action %"PRIu16" wrong length: %zu***\n", type, len);
338 return -1;
339 }
340 }
d295e8e9 341
064af421
BP
342 switch (type) {
343 case OFPAT_OUTPUT: {
344 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
d295e8e9 345 uint16_t port = ntohs(oa->port);
064af421
BP
346 if (port < OFPP_MAX) {
347 ds_put_format(string, "output:%"PRIu16, port);
348 } else {
349 ofp_print_port_name(string, port);
350 if (port == OFPP_CONTROLLER) {
351 if (oa->max_len) {
352 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
353 } else {
354 ds_put_cstr(string, ":all");
355 }
356 }
357 }
358 break;
359 }
360
c4d279ab
BP
361 case OFPAT_ENQUEUE: {
362 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
363 unsigned int port = ntohs(ea->port);
364 unsigned int queue_id = ntohl(ea->queue_id);
365 ds_put_format(string, "enqueue:");
366 if (port != OFPP_IN_PORT) {
367 ds_put_format(string, "%u", port);
368 } else {
369 ds_put_cstr(string, "IN_PORT");
370 }
371 ds_put_format(string, "q%u", queue_id);
372 break;
373 }
374
064af421
BP
375 case OFPAT_SET_VLAN_VID: {
376 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
377 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
378 break;
379 }
380
381 case OFPAT_SET_VLAN_PCP: {
382 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
383 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
384 break;
385 }
386
387 case OFPAT_STRIP_VLAN:
388 ds_put_cstr(string, "strip_vlan");
389 break;
390
391 case OFPAT_SET_DL_SRC: {
392 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
d295e8e9 393 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
064af421
BP
394 ETH_ADDR_ARGS(da->dl_addr));
395 break;
396 }
397
398 case OFPAT_SET_DL_DST: {
399 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
d295e8e9 400 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
064af421
BP
401 ETH_ADDR_ARGS(da->dl_addr));
402 break;
403 }
404
405 case OFPAT_SET_NW_SRC: {
406 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
407 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
408 break;
409 }
410
411 case OFPAT_SET_NW_DST: {
412 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
413 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
414 break;
415 }
416
959a2ecd
JP
417 case OFPAT_SET_NW_TOS: {
418 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
419 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
420 break;
421 }
422
064af421
BP
423 case OFPAT_SET_TP_SRC: {
424 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
425 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
426 break;
427 }
428
429 case OFPAT_SET_TP_DST: {
430 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
431 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
432 break;
433 }
434
435 case OFPAT_VENDOR: {
d295e8e9 436 struct ofp_action_vendor_header *avh
064af421
BP
437 = (struct ofp_action_vendor_header *)ah;
438 if (len < sizeof *avh) {
439 ds_put_format(string, "***ofpat_vendor truncated***\n");
440 return -1;
441 }
442 if (avh->vendor == htonl(NX_VENDOR_ID)) {
443 ofp_print_nx_action(string, (struct nx_action_header *)avh);
444 } else {
445 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
446 }
447 break;
448 }
449
450 default:
451 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
452 break;
453 }
454
455 return len;
456}
457
d295e8e9 458void
064af421 459ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
d295e8e9 460 size_t actions_len)
064af421
BP
461{
462 uint8_t *p = (uint8_t *)action;
463 int len = 0;
464
465 ds_put_cstr(string, "actions=");
466 if (!actions_len) {
467 ds_put_cstr(string, "drop");
468 }
469 while (actions_len > 0) {
470 if (len) {
471 ds_put_cstr(string, ",");
472 }
d295e8e9 473 len = ofp_print_action(string, (struct ofp_action_header *)p,
064af421
BP
474 actions_len);
475 if (len < 0) {
476 return;
477 }
478 p += len;
479 actions_len -= len;
480 }
481}
482
d1e2cf21
BP
483static void
484ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
485 int verbosity)
064af421 486{
d1e2cf21 487 size_t len = ntohs(opo->header.length);
064af421
BP
488 size_t actions_len = ntohs(opo->actions_len);
489
490 ds_put_cstr(string, " in_port=");
491 ofp_print_port_name(string, ntohs(opo->in_port));
492
493 ds_put_format(string, " actions_len=%zu ", actions_len);
494 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
495 ds_put_format(string, "***packet too short for action length***\n");
496 return;
497 }
498 ofp_print_actions(string, opo->actions, actions_len);
499
500 if (ntohl(opo->buffer_id) == UINT32_MAX) {
501 int data_len = len - sizeof *opo - actions_len;
502 ds_put_format(string, " data_len=%d", data_len);
503 if (verbosity > 0 && len > sizeof *opo) {
504 char *packet = ofp_packet_to_string(
505 (uint8_t *)opo->actions + actions_len, data_len, data_len);
506 ds_put_char(string, '\n');
507 ds_put_cstr(string, packet);
508 free(packet);
509 }
510 } else {
511 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
512 }
513 ds_put_char(string, '\n');
514}
515
516/* qsort comparison function. */
517static int
518compare_ports(const void *a_, const void *b_)
519{
520 const struct ofp_phy_port *a = a_;
521 const struct ofp_phy_port *b = b_;
522 uint16_t ap = ntohs(a->port_no);
523 uint16_t bp = ntohs(b->port_no);
524
525 return ap < bp ? -1 : ap > bp;
526}
527
528static void ofp_print_port_features(struct ds *string, uint32_t features)
529{
530 if (features == 0) {
531 ds_put_cstr(string, "Unsupported\n");
532 return;
533 }
534 if (features & OFPPF_10MB_HD) {
535 ds_put_cstr(string, "10MB-HD ");
536 }
537 if (features & OFPPF_10MB_FD) {
538 ds_put_cstr(string, "10MB-FD ");
539 }
540 if (features & OFPPF_100MB_HD) {
541 ds_put_cstr(string, "100MB-HD ");
542 }
543 if (features & OFPPF_100MB_FD) {
544 ds_put_cstr(string, "100MB-FD ");
545 }
546 if (features & OFPPF_1GB_HD) {
547 ds_put_cstr(string, "1GB-HD ");
548 }
549 if (features & OFPPF_1GB_FD) {
550 ds_put_cstr(string, "1GB-FD ");
551 }
552 if (features & OFPPF_10GB_FD) {
553 ds_put_cstr(string, "10GB-FD ");
554 }
555 if (features & OFPPF_COPPER) {
556 ds_put_cstr(string, "COPPER ");
557 }
558 if (features & OFPPF_FIBER) {
559 ds_put_cstr(string, "FIBER ");
560 }
561 if (features & OFPPF_AUTONEG) {
562 ds_put_cstr(string, "AUTO_NEG ");
563 }
564 if (features & OFPPF_PAUSE) {
565 ds_put_cstr(string, "AUTO_PAUSE ");
566 }
567 if (features & OFPPF_PAUSE_ASYM) {
568 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
569 }
570 ds_put_char(string, '\n');
571}
572
573static void
574ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
575{
0b61210e 576 char name[OFP_MAX_PORT_NAME_LEN];
064af421
BP
577 int j;
578
579 memcpy(name, port->name, sizeof name);
580 for (j = 0; j < sizeof name - 1; j++) {
581 if (!isprint(name[j])) {
582 break;
583 }
584 }
585 name[j] = '\0';
586
587 ds_put_char(string, ' ');
588 ofp_print_port_name(string, ntohs(port->port_no));
589 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
590 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
591 ntohl(port->state));
592 if (port->curr) {
593 ds_put_format(string, " current: ");
594 ofp_print_port_features(string, ntohl(port->curr));
595 }
596 if (port->advertised) {
597 ds_put_format(string, " advertised: ");
598 ofp_print_port_features(string, ntohl(port->advertised));
599 }
600 if (port->supported) {
601 ds_put_format(string, " supported: ");
602 ofp_print_port_features(string, ntohl(port->supported));
603 }
604 if (port->peer) {
605 ds_put_format(string, " peer: ");
606 ofp_print_port_features(string, ntohl(port->peer));
607 }
608}
609
064af421 610static void
d1e2cf21
BP
611ofp_print_switch_features(struct ds *string,
612 const struct ofp_switch_features *osf)
064af421 613{
d1e2cf21 614 size_t len = ntohs(osf->header.length);
064af421
BP
615 struct ofp_phy_port *port_list;
616 int n_ports;
617 int i;
618
d295e8e9 619 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
064af421
BP
620 osf->header.version, ntohll(osf->datapath_id));
621 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
622 ntohl(osf->n_buffers));
623 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
624 ntohl(osf->capabilities), ntohl(osf->actions));
625
064af421
BP
626 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
627
d295e8e9 628 port_list = xmemdup(osf->ports, len - sizeof *osf);
064af421
BP
629 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
630 for (i = 0; i < n_ports; i++) {
631 ofp_print_phy_port(string, &port_list[i]);
632 }
633 free(port_list);
634}
635
064af421 636static void
d1e2cf21 637ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
064af421 638{
064af421
BP
639 uint16_t flags;
640
641 flags = ntohs(osc->flags);
064af421
BP
642 if (flags) {
643 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
644 }
645
646 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
647}
648
649static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 650 int verbosity, const char *format, ...)
064af421
BP
651 __attribute__((format(printf, 5, 6)));
652
653static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 654 int verbosity, const char *format, ...)
064af421
BP
655{
656 if (is_wild && verbosity < 2) {
657 return;
658 }
659 ds_put_cstr(string, leader);
660 if (!is_wild) {
661 va_list args;
662
663 va_start(args, format);
664 ds_put_format_valist(string, format, args);
665 va_end(args);
666 } else {
667 ds_put_char(string, '*');
668 }
669 ds_put_char(string, ',');
670}
671
672static void
673print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
674 uint32_t wild_bits, int verbosity)
675{
676 if (wild_bits >= 32 && verbosity < 2) {
677 return;
678 }
679 ds_put_cstr(string, leader);
680 if (wild_bits < 32) {
681 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
682 if (wild_bits) {
683 ds_put_format(string, "/%d", 32 - wild_bits);
684 }
685 } else {
686 ds_put_char(string, '*');
687 }
688 ds_put_char(string, ',');
689}
690
4f2cad2c 691void
064af421
BP
692ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
693{
694 char *s = ofp_match_to_string(om, verbosity);
695 ds_put_cstr(f, s);
696 free(s);
697}
698
699char *
700ofp_match_to_string(const struct ofp_match *om, int verbosity)
701{
702 struct ds f = DS_EMPTY_INITIALIZER;
703 uint32_t w = ntohl(om->wildcards);
704 bool skip_type = false;
705 bool skip_proto = false;
706
707 if (!(w & OFPFW_DL_TYPE)) {
708 skip_type = true;
709 if (om->dl_type == htons(ETH_TYPE_IP)) {
710 if (!(w & OFPFW_NW_PROTO)) {
711 skip_proto = true;
712 if (om->nw_proto == IP_TYPE_ICMP) {
713 ds_put_cstr(&f, "icmp,");
714 } else if (om->nw_proto == IP_TYPE_TCP) {
715 ds_put_cstr(&f, "tcp,");
716 } else if (om->nw_proto == IP_TYPE_UDP) {
717 ds_put_cstr(&f, "udp,");
718 } else {
719 ds_put_cstr(&f, "ip,");
720 skip_proto = false;
721 }
722 } else {
723 ds_put_cstr(&f, "ip,");
724 }
725 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
726 ds_put_cstr(&f, "arp,");
727 } else {
728 skip_type = false;
729 }
730 }
659586ef
JG
731 if (w & NXFW_TUN_ID) {
732 ds_put_cstr(&f, "tun_id_wild,");
733 }
064af421
BP
734 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
735 "%d", ntohs(om->in_port));
736 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
6a1f89c8 737 "%d", ntohs(om->dl_vlan));
959a2ecd
JP
738 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
739 "%d", om->dl_vlan_pcp);
064af421
BP
740 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
741 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
742 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
743 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
744 if (!skip_type) {
745 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
746 "0x%04x", ntohs(om->dl_type));
747 }
748 print_ip_netmask(&f, "nw_src=", om->nw_src,
749 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
750 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
751 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
752 if (!skip_proto) {
fb892732
JP
753 if (om->dl_type == htons(ETH_TYPE_ARP)) {
754 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
755 "%u", om->nw_proto);
756 } else {
757 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
758 "%u", om->nw_proto);
759 }
064af421 760 }
1a960c80
RL
761 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
762 "%u", om->nw_tos);
064af421
BP
763 if (om->nw_proto == IP_TYPE_ICMP) {
764 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
765 "%d", ntohs(om->icmp_type));
766 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
767 "%d", ntohs(om->icmp_code));
768 } else {
769 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
770 "%d", ntohs(om->tp_src));
771 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
772 "%d", ntohs(om->tp_dst));
773 }
7fa91113
BP
774 if (ds_last(&f) == ',') {
775 f.length--;
776 }
064af421
BP
777 return ds_cstr(&f);
778}
779
064af421 780static void
7fa91113
BP
781ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
782 enum ofputil_msg_code code, int verbosity)
064af421 783{
7fa91113
BP
784 struct flow_mod fm;
785 int error;
064af421 786
7fa91113
BP
787 error = ofputil_decode_flow_mod(&fm, oh, NXFF_OPENFLOW10);
788 if (error) {
789 ofp_print_error(s, error);
790 return;
3ff4f871
BP
791 }
792
7fa91113
BP
793 ds_put_char(s, ' ');
794 switch (fm.command) {
064af421 795 case OFPFC_ADD:
7fa91113 796 ds_put_cstr(s, "ADD");
064af421
BP
797 break;
798 case OFPFC_MODIFY:
7fa91113 799 ds_put_cstr(s, "MOD");
064af421
BP
800 break;
801 case OFPFC_MODIFY_STRICT:
7fa91113 802 ds_put_cstr(s, "MOD_STRICT");
064af421
BP
803 break;
804 case OFPFC_DELETE:
7fa91113 805 ds_put_cstr(s, "DEL");
064af421
BP
806 break;
807 case OFPFC_DELETE_STRICT:
7fa91113 808 ds_put_cstr(s, "DEL_STRICT");
064af421
BP
809 break;
810 default:
7fa91113
BP
811 ds_put_format(s, "cmd:%d", fm.command);
812 }
813
814 ds_put_char(s, ' ');
815 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
816 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
817
818 ofp_print_match(s, &ofm->match, verbosity);
819 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
820 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
821 const void *nxm = nfm + 1;
822 char *nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
823 ds_put_cstr(s, nxm_s);
824 free(nxm_s);
825 } else {
826 cls_rule_format(&fm.cr, s);
3ff4f871 827 }
7fa91113
BP
828
829 if (ds_last(s) != ' ') {
830 ds_put_char(s, ' ');
3ff4f871 831 }
7fa91113
BP
832 if (fm.cookie != htonll(0)) {
833 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
3ff4f871 834 }
7fa91113
BP
835 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
836 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
064af421 837 }
7fa91113
BP
838 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
839 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
3ff4f871 840 }
7fa91113
BP
841 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && verbosity >= 3) {
842 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
3ff4f871 843 }
7fa91113
BP
844 if (fm.buffer_id != UINT32_MAX) {
845 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
3ff4f871 846 }
7fa91113
BP
847 if (fm.flags != 0) {
848 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
849 }
850
851 ofp_print_actions(s, (const struct ofp_action_header *) fm.actions,
852 fm.n_actions * sizeof *fm.actions);
064af421
BP
853}
854
09862ec6
BP
855static void
856ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
857{
858 ds_put_format(string, "%u", sec);
859 if (nsec > 0) {
860 ds_put_format(string, ".%09u", nsec);
861 while (string->string[string->length - 1] == '0') {
862 string->length--;
863 }
864 }
865 ds_put_char(string, 's');
866}
867
064af421 868static void
9b045a0c 869ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
064af421 870{
9b045a0c
BP
871 struct ofputil_flow_removed fr;
872 int error;
873
874 error = ofputil_decode_flow_removed(&fr, oh, NXFF_OPENFLOW10);
875 if (error) {
876 ofp_print_error(string, error);
877 return;
878 }
879
880 cls_rule_format(&fr.rule, string);
881
064af421 882 ds_put_cstr(string, " reason=");
9b045a0c 883 switch (fr.reason) {
ca069229 884 case OFPRR_IDLE_TIMEOUT:
064af421
BP
885 ds_put_cstr(string, "idle");
886 break;
ca069229 887 case OFPRR_HARD_TIMEOUT:
064af421
BP
888 ds_put_cstr(string, "hard");
889 break;
ca069229
JP
890 case OFPRR_DELETE:
891 ds_put_cstr(string, "delete");
892 break;
064af421 893 default:
9b045a0c 894 ds_put_format(string, "**%"PRIu8"**", fr.reason);
064af421
BP
895 break;
896 }
3ff4f871 897
9b045a0c
BP
898 if (fr.cookie != htonll(0)) {
899 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
3ff4f871 900 }
09862ec6 901 ds_put_cstr(string, " duration");
9b045a0c 902 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
09862ec6 903 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
9b045a0c 904 fr.idle_timeout, fr.packet_count, fr.byte_count);
064af421
BP
905}
906
907static void
d1e2cf21 908ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
064af421 909{
064af421 910 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
d295e8e9 911 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
064af421
BP
912 ntohl(opm->config), ntohl(opm->mask));
913 ds_put_format(string, " advertise: ");
914 if (opm->advertise) {
915 ofp_print_port_features(string, ntohl(opm->advertise));
916 } else {
917 ds_put_format(string, "UNCHANGED\n");
918 }
919}
920
921struct error_type {
922 int type;
923 int code;
924 const char *name;
925};
926
927static const struct error_type error_types[] = {
928#define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
929#define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
930 ERROR_TYPE(OFPET_HELLO_FAILED),
931 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
49bdc010 932 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_EPERM),
064af421
BP
933
934 ERROR_TYPE(OFPET_BAD_REQUEST),
935 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
936 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
937 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
b4785c1e
JP
938 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
939 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
49bdc010
JP
940 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_EPERM),
941 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN),
b4785c1e 942 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
49bdc010 943 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN),
f74be05a
JP
944 /* Nicira error extensions. */
945 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
946 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
947 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
948 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
949 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
950 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
064af421
BP
951
952 ERROR_TYPE(OFPET_BAD_ACTION),
953 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
954 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
955 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
956 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
957 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
b4785c1e 958 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
49bdc010 959 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_EPERM),
b4785c1e 960 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
064af421
BP
961
962 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
b4785c1e 963 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
49bdc010
JP
964 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP),
965 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_EPERM),
966 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT),
b4785c1e 967 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
f74be05a
JP
968 /* Nicira error extenstions. */
969 ERROR_CODE(OFPET_FLOW_MOD_FAILED, NXFMFC_HARDWARE),
970 ERROR_CODE(OFPET_FLOW_MOD_FAILED, NXFMFC_BAD_TABLE_ID),
b4785c1e
JP
971
972 ERROR_TYPE(OFPET_PORT_MOD_FAILED),
973 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
974 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
064af421
BP
975};
976#define N_ERROR_TYPES ARRAY_SIZE(error_types)
977
978static const char *
979lookup_error_type(int type)
980{
981 const struct error_type *t;
982
983 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
984 if (t->type == type && t->code == -1) {
985 return t->name;
986 }
987 }
988 return "?";
989}
990
991static const char *
992lookup_error_code(int type, int code)
993{
994 const struct error_type *t;
995
996 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
997 if (t->type == type && t->code == code) {
998 return t->name;
999 }
1000 }
1001 return "?";
1002}
1003
7fa91113
BP
1004static void
1005ofp_print_error(struct ds *string, int error)
1006{
1007 int type = get_ofp_err_type(error);
1008 int code = get_ofp_err_code(error);
1009 if (string->length) {
1010 ds_put_char(string, ' ');
1011 }
1012 ds_put_format(string, " ***decode error type:%d(%s) code:%d(%s)***",
1013 type, lookup_error_type(type),
1014 code, lookup_error_code(type, code));
1015}
1016
f74be05a
JP
1017static void
1018ofp_print_nx_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1019{
1020 size_t len = ntohs(oem->header.length);
1021 const struct nx_vendor_error *nve = (struct nx_vendor_error *)oem->data;
1022 int vendor = ntohl(nve->vendor);
1023 int type = ntohs(nve->type);
1024 int code = ntohs(nve->code);
1025
1026 ds_put_format(string, " vendor:%x type:%d(%s) code:%d(%s) payload:\n",
1027 vendor,
1028 type, lookup_error_type(type),
1029 code, lookup_error_code(type, code));
1030
1031 ds_put_hex_dump(string, nve + 1, len - sizeof *oem - sizeof *nve,
1032 0, true);
1033}
1034
064af421 1035static void
d1e2cf21 1036ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
064af421 1037{
d1e2cf21 1038 size_t len = ntohs(oem->header.length);
064af421
BP
1039 int type = ntohs(oem->type);
1040 int code = ntohs(oem->code);
1041 char *s;
1042
f74be05a
JP
1043
1044 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
1045 if (len < sizeof *oem + sizeof(struct nx_vendor_error)) {
d689a6a8 1046 ds_put_format(string,
f74be05a 1047 "(***truncated extended error message is %zu bytes "
d689a6a8 1048 "when it should be at least %zu***)\n",
f74be05a
JP
1049 len, sizeof(struct nx_vendor_error));
1050 return;
1051 }
1052
1053 return ofp_print_nx_error_msg(string, oem);
1054 }
1055
7fa91113 1056 ds_put_format(string, " type:%d(%s) code:%d(%s) payload:\n",
064af421
BP
1057 type, lookup_error_type(type),
1058 code, lookup_error_code(type, code));
1059
1060 switch (type) {
1061 case OFPET_HELLO_FAILED:
1062 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
1063 break;
1064
1065 case OFPET_BAD_REQUEST:
1066 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
1067 ds_put_cstr(string, s);
1068 free(s);
1069 break;
1070
1071 default:
1072 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1073 break;
1074 }
1075}
1076
064af421 1077static void
d1e2cf21 1078ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
064af421 1079{
064af421
BP
1080 if (ops->reason == OFPPR_ADD) {
1081 ds_put_format(string, " ADD:");
1082 } else if (ops->reason == OFPPR_DELETE) {
1083 ds_put_format(string, " DEL:");
1084 } else if (ops->reason == OFPPR_MODIFY) {
1085 ds_put_format(string, " MOD:");
1086 }
1087
1088 ofp_print_phy_port(string, &ops->desc);
1089}
1090
1091static void
d1e2cf21 1092ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
064af421 1093{
d1e2cf21 1094 const struct ofp_desc_stats *ods = ofputil_stats_body(oh);
064af421 1095
d295e8e9 1096 ds_put_format(string, "Manufacturer: %.*s\n",
dd70b475
JP
1097 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1098 ds_put_format(string, "Hardware: %.*s\n",
1099 (int) sizeof ods->hw_desc, ods->hw_desc);
1100 ds_put_format(string, "Software: %.*s\n",
1101 (int) sizeof ods->sw_desc, ods->sw_desc);
1102 ds_put_format(string, "Serial Num: %.*s\n",
1103 (int) sizeof ods->serial_num, ods->serial_num);
1104 ds_put_format(string, "DP Description: %.*s\n",
1105 (int) sizeof ods->dp_desc, ods->dp_desc);
064af421
BP
1106}
1107
1108static void
2af0c0ef 1109ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
064af421 1110{
2af0c0ef
BP
1111 struct flow_stats_request fsr;
1112 int error;
064af421 1113
2af0c0ef
BP
1114 error = ofputil_decode_flow_stats_request(&fsr, oh, NXFF_OPENFLOW10);
1115 if (error) {
1116 ofp_print_error(string, error);
1117 return;
1118 }
1119
1120 if (fsr.table_id != 0xff) {
1121 ds_put_format(string, " table_id=%"PRIu8, fsr.table_id);
064af421
BP
1122 }
1123
2af0c0ef
BP
1124 if (fsr.out_port != OFPP_NONE) {
1125 ds_put_cstr(string, " out_port=");
1126 ofp_print_port_name(string, fsr.out_port);
1127 }
1128
1129 ds_put_char(string, ' ');
1130 cls_rule_format(&fsr.match, string);
064af421
BP
1131}
1132
1133static void
d1e2cf21
BP
1134ofp_print_ofpst_flow_reply(struct ds *string, const struct ofp_header *oh,
1135 int verbosity)
064af421 1136{
d1e2cf21
BP
1137 size_t len = ofputil_stats_body_len(oh);
1138 const char *body = ofputil_stats_body(oh);
064af421
BP
1139 const char *pos = body;
1140 for (;;) {
1141 const struct ofp_flow_stats *fs;
1142 ptrdiff_t bytes_left = body + len - pos;
1143 size_t length;
1144
fab8fadb
BP
1145 ds_put_char(string, '\n');
1146
064af421
BP
1147 if (bytes_left < sizeof *fs) {
1148 if (bytes_left != 0) {
1149 ds_put_format(string, " ***%td leftover bytes at end***",
1150 bytes_left);
1151 }
1152 break;
1153 }
1154
1155 fs = (const void *) pos;
1156 length = ntohs(fs->length);
1157 if (length < sizeof *fs) {
1158 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
1159 length, sizeof *fs);
1160 break;
1161 } else if (length > bytes_left) {
1162 ds_put_format(string,
1163 " ***length=%zu but only %td bytes left***",
1164 length, bytes_left);
1165 break;
1166 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1167 ds_put_format(string,
1168 " ***length=%zu has %zu bytes leftover in "
1169 "final action***",
1170 length,
1171 (length - sizeof *fs) % sizeof fs->actions[0]);
1172 break;
1173 }
1174
09862ec6
BP
1175 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1176 ntohll(fs->cookie));
1177 ofp_print_duration(string, ntohl(fs->duration_sec),
1178 ntohl(fs->duration_nsec));
1179 ds_put_format(string, ", table_id=%"PRIu8", ", fs->table_id);
05abb769 1180 ds_put_format(string, "priority=%"PRIu16", ", ntohs(fs->priority));
064af421
BP
1181 ds_put_format(string, "n_packets=%"PRIu64", ",
1182 ntohll(fs->packet_count));
1183 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1184 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1185 ds_put_format(string, "idle_timeout=%"PRIu16",",
1186 ntohs(fs->idle_timeout));
1187 }
1188 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1189 ds_put_format(string, "hard_timeout=%"PRIu16",",
1190 ntohs(fs->hard_timeout));
1191 }
1192 ofp_print_match(string, &fs->match, verbosity);
05abb769 1193 ds_put_char(string, ' ');
064af421 1194 ofp_print_actions(string, fs->actions, length - sizeof *fs);
064af421
BP
1195
1196 pos += length;
1197 }
1198}
1199
c6430da5
BP
1200static void
1201ofp_print_nxst_flow_reply(struct ds *string, const struct ofp_header *oh)
1202{
1203 struct ofpbuf b;
1204
1205 ofpbuf_use_const(&b, ofputil_nxstats_body(oh),
1206 ofputil_nxstats_body_len(oh));
1207 while (b.size > 0) {
1208 const struct nx_flow_stats *fs;
1209 union ofp_action *actions;
1210 struct cls_rule rule;
1211 size_t actions_len, n_actions;
1212 size_t length;
1213 int match_len;
1214 int error;
1215
1216 fs = ofpbuf_try_pull(&b, sizeof *fs);
1217 if (!fs) {
1218 ds_put_format(string, " ***%td leftover bytes at end***", b.size);
1219 break;
1220 }
1221
1222 length = ntohs(fs->length);
1223 if (length < sizeof *fs) {
1224 ds_put_format(string, " ***nx_flow_stats claims length %zu***",
1225 length);
1226 break;
1227 }
1228
1229 match_len = ntohs(fs->match_len);
1230 if (match_len > length - sizeof *fs) {
1231 ds_put_format(string, " ***length=%zu match_len=%d***",
1232 length, match_len);
1233 break;
1234 }
1235
09862ec6
BP
1236 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1237 ntohll(fs->cookie));
1238 ofp_print_duration(string, ntohl(fs->duration_sec),
1239 ntohl(fs->duration_nsec));
1240 ds_put_format(string, ", table_id=%"PRIu8", ", fs->table_id);
c6430da5
BP
1241 ds_put_format(string, "priority=%"PRIu16", ", ntohs(fs->priority));
1242 ds_put_format(string, "n_packets=%"PRIu64", ",
1243 ntohll(fs->packet_count));
1244 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1245 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1246 ds_put_format(string, "idle_timeout=%"PRIu16",",
1247 ntohs(fs->idle_timeout));
1248 }
1249 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1250 ds_put_format(string, "hard_timeout=%"PRIu16",",
1251 ntohs(fs->hard_timeout));
1252 }
1253
1254 error = nx_pull_match(&b, match_len, ntohs(fs->priority), &rule);
1255 if (error) {
1256 ofp_print_error(string, error);
1257 break;
1258 }
1259
1260 actions_len = length - sizeof *fs - ROUND_UP(match_len, 8);
1261 error = ofputil_pull_actions(&b, actions_len, &actions, &n_actions);
1262 if (error) {
1263 ofp_print_error(string, error);
1264 break;
1265 }
1266
1267 cls_rule_format(&rule, string);
1268 ds_put_char(string, ' ');
1269 ofp_print_actions(string, (const struct ofp_action_header *) actions,
1270 n_actions * sizeof *actions);
1271 ds_put_char(string, '\n');
1272 }
1273}
1274
064af421 1275static void
a2ad9ecd
BP
1276ofp_print_ofp_aggregate_stats_reply (
1277 struct ds *string, const struct ofp_aggregate_stats_reply *asr)
064af421 1278{
064af421
BP
1279 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1280 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1281 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1282}
1283
a2ad9ecd
BP
1284static void
1285ofp_print_ofpst_aggregate_reply(struct ds *string, const struct ofp_header *oh)
1286{
1287 ofp_print_ofp_aggregate_stats_reply(string, ofputil_stats_body(oh));
1288}
1289
1290static void
1291ofp_print_nxst_aggregate_reply(struct ds *string,
1292 const struct nx_aggregate_stats_reply *nasr)
1293{
1294 ofp_print_ofp_aggregate_stats_reply(string, &nasr->asr);
1295}
1296
d295e8e9 1297static void print_port_stat(struct ds *string, const char *leader,
064af421
BP
1298 uint64_t stat, int more)
1299{
1300 ds_put_cstr(string, leader);
1301 if (stat != -1) {
1302 ds_put_format(string, "%"PRIu64, stat);
1303 } else {
1304 ds_put_char(string, '?');
1305 }
1306 if (more) {
1307 ds_put_cstr(string, ", ");
1308 } else {
1309 ds_put_cstr(string, "\n");
1310 }
1311}
1312
abaad8cf 1313static void
d1e2cf21 1314ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
abaad8cf 1315{
d1e2cf21 1316 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
abaad8cf
JP
1317 ds_put_format(string, "port_no=%"PRIu16, ntohs(psr->port_no));
1318}
1319
064af421 1320static void
d1e2cf21
BP
1321ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1322 int verbosity)
064af421 1323{
d1e2cf21
BP
1324 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1325 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
064af421
BP
1326 ds_put_format(string, " %zu ports\n", n);
1327 if (verbosity < 1) {
1328 return;
1329 }
1330
1331 for (; n--; ps++) {
1332 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1333
1334 ds_put_cstr(string, "rx ");
1335 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1336 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1337 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1338 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1339 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1340 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1341 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1342
1343 ds_put_cstr(string, " tx ");
1344 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1345 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1346 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1347 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1348 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1349 }
1350}
1351
1352static void
d1e2cf21
BP
1353ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1354 int verbosity)
064af421 1355{
d1e2cf21
BP
1356 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1357 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
064af421
BP
1358 ds_put_format(string, " %zu tables\n", n);
1359 if (verbosity < 1) {
1360 return;
1361 }
1362
1363 for (; n--; ts++) {
1364 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1365 strncpy(name, ts->name, sizeof name);
1366 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1367
1368 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1369 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1370 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1371 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1372 ds_put_cstr(string, " ");
d295e8e9 1373 ds_put_format(string, "lookup=%"PRIu64", ",
064af421
BP
1374 ntohll(ts->lookup_count));
1375 ds_put_format(string, "matched=%"PRIu64"\n",
1376 ntohll(ts->matched_count));
1377 }
1378}
1379
d2805da2
BP
1380static void
1381ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1382{
1383 if (queue_id == OFPQ_ALL) {
1384 ds_put_cstr(string, "ALL");
1385 } else {
1386 ds_put_format(string, "%"PRIu32, queue_id);
1387 }
1388}
1389
1390static void
d1e2cf21 1391ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
d2805da2 1392{
d1e2cf21 1393 const struct ofp_queue_stats_request *qsr = ofputil_stats_body(oh);
d2805da2
BP
1394
1395 ds_put_cstr(string, "port=");
1396 ofp_print_port_name(string, ntohs(qsr->port_no));
1397
1398 ds_put_cstr(string, " queue=");
1399 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1400}
1401
1402static void
d1e2cf21
BP
1403ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1404 int verbosity)
d2805da2 1405{
d1e2cf21
BP
1406 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1407 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
d2805da2
BP
1408 ds_put_format(string, " %zu queues\n", n);
1409 if (verbosity < 1) {
1410 return;
1411 }
1412
1413 for (; n--; qs++) {
1414 ds_put_cstr(string, " port ");
1415 ofp_print_port_name(string, ntohs(qs->port_no));
1416 ds_put_cstr(string, " queue ");
1417 ofp_print_queue_name(string, ntohl(qs->queue_id));
1418 ds_put_cstr(string, ": ");
1419
1420 print_port_stat(string, "bytes=", ntohll(qs->tx_bytes), 1);
1421 print_port_stat(string, "pkts=", ntohll(qs->tx_packets), 1);
1422 print_port_stat(string, "errors=", ntohll(qs->tx_errors), 0);
1423 }
1424}
1425
064af421 1426static void
d1e2cf21 1427ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
064af421 1428{
d1e2cf21
BP
1429 const struct ofp_stats_request *srq
1430 = (const struct ofp_stats_request *) oh;
064af421
BP
1431
1432 if (srq->flags) {
1433 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1434 ntohs(srq->flags));
1435 }
064af421
BP
1436}
1437
1438static void
d1e2cf21 1439ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
064af421 1440{
d1e2cf21 1441 const struct ofp_stats_reply *srp = (const struct ofp_stats_reply *) oh;
064af421 1442
d1e2cf21 1443 if (srp->flags) {
064af421 1444 uint16_t flags = ntohs(srp->flags);
d1e2cf21
BP
1445
1446 ds_put_cstr(string, " flags=");
064af421
BP
1447 if (flags & OFPSF_REPLY_MORE) {
1448 ds_put_cstr(string, "[more]");
1449 flags &= ~OFPSF_REPLY_MORE;
1450 }
1451 if (flags) {
d1e2cf21
BP
1452 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1453 flags);
064af421
BP
1454 }
1455 }
064af421
BP
1456}
1457
1458static void
d1e2cf21 1459ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
064af421 1460{
d1e2cf21 1461 size_t len = ntohs(oh->length);
064af421 1462
d1e2cf21 1463 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
064af421 1464 if (verbosity > 1) {
d1e2cf21 1465 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
064af421
BP
1466 }
1467}
1468
3731490f
BP
1469static void
1470ofp_print_nxt_status_message(struct ds *string, const struct ofp_header *oh)
1471{
1472 struct ofpbuf b;
1473
1474 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1475 ofpbuf_pull(&b, sizeof *oh);
1476 ds_put_char(string, '"');
1477 ds_put_printable(string, b.data, b.size);
1478 ds_put_char(string, '"');
1479}
1480
7fa91113
BP
1481static void
1482ofp_print_nxt_tun_id_from_cookie(struct ds *string,
1483 const struct nxt_tun_id_cookie *ntic)
1484{
1485 ds_put_format(string, " set=%"PRIu8, ntic->set);
1486}
1487
61fe3a7b
BP
1488static void
1489ofp_print_nxt_role_message(struct ds *string,
1490 const struct nx_role_request *nrr)
1491{
1492 unsigned int role = ntohl(nrr->role);
1493
1494 ds_put_cstr(string, " role=");
1495 if (role == NX_ROLE_OTHER) {
1496 ds_put_cstr(string, "other");
1497 } else if (role == NX_ROLE_MASTER) {
1498 ds_put_cstr(string, "master");
1499 } else if (role == NX_ROLE_SLAVE) {
1500 ds_put_cstr(string, "slave");
1501 } else {
1502 ds_put_format(string, "%u", role);
1503 }
1504}
1505
7fa91113
BP
1506static void
1507ofp_print_nxt_set_flow_format(struct ds *string,
1508 const struct nxt_set_flow_format *nsff)
1509{
1510 uint32_t format = ntohl(nsff->format);
1511
1512 ds_put_cstr(string, " format=");
1513 if (ofputil_flow_format_is_valid(format)) {
1514 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1515 } else {
1516 ds_put_format(string, "%"PRIu32, format);
1517 }
1518}
1519
d1e2cf21
BP
1520static void
1521ofp_to_string__(const struct ofp_header *oh,
1522 const struct ofputil_msg_type *type, struct ds *string,
1523 int verbosity)
1524{
7fa91113 1525 enum ofputil_msg_code code;
d1e2cf21
BP
1526 const void *msg = oh;
1527
1528 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1529 ofputil_msg_type_name(type), ntohl(oh->xid));
1530
7fa91113
BP
1531 code = ofputil_msg_type_code(type);
1532 switch (code) {
d1e2cf21
BP
1533 case OFPUTIL_INVALID:
1534 break;
1535
1536 case OFPUTIL_OFPT_HELLO:
1537 break;
1538
1539 case OFPUTIL_OFPT_ERROR:
1540 ofp_print_error_msg(string, msg);
1541 break;
1542
1543 case OFPUTIL_OFPT_ECHO_REQUEST:
1544 case OFPUTIL_OFPT_ECHO_REPLY:
1545 ofp_print_echo(string, oh, verbosity);
1546 break;
1547
1548 case OFPUTIL_OFPT_FEATURES_REQUEST:
1549 break;
1550
1551 case OFPUTIL_OFPT_FEATURES_REPLY:
1552 ofp_print_switch_features(string, msg);
1553 break;
1554
1555 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1556 break;
1557
1558 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1559 case OFPUTIL_OFPT_SET_CONFIG:
1560 ofp_print_switch_config(string, msg);
1561 break;
1562
1563 case OFPUTIL_OFPT_PACKET_IN:
1564 ofp_print_packet_in(string, msg, verbosity);
1565 break;
1566
1567 case OFPUTIL_OFPT_FLOW_REMOVED:
9b045a0c
BP
1568 case OFPUTIL_NXT_FLOW_REMOVED:
1569 ofp_print_flow_removed(string, msg);
d1e2cf21
BP
1570 break;
1571
1572 case OFPUTIL_OFPT_PORT_STATUS:
1573 ofp_print_port_status(string, msg);
1574 break;
1575
1576 case OFPUTIL_OFPT_PACKET_OUT:
1577 ofp_print_packet_out(string, msg, verbosity);
1578 break;
1579
1580 case OFPUTIL_OFPT_FLOW_MOD:
7fa91113 1581 ofp_print_flow_mod(string, msg, code, verbosity);
d1e2cf21
BP
1582 break;
1583
1584 case OFPUTIL_OFPT_PORT_MOD:
1585 ofp_print_port_mod(string, msg);
1586 break;
1587
1588 case OFPUTIL_OFPT_BARRIER_REQUEST:
1589 case OFPUTIL_OFPT_BARRIER_REPLY:
1590 break;
1591
1592 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1593 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1594 /* XXX */
1595 break;
1596
1597 case OFPUTIL_OFPST_DESC_REQUEST:
1598 ofp_print_stats_request(string, oh);
1599 break;
1600
1601 case OFPUTIL_OFPST_FLOW_REQUEST:
2af0c0ef 1602 case OFPUTIL_NXST_FLOW_REQUEST:
d1e2cf21 1603 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
2af0c0ef 1604 case OFPUTIL_NXST_AGGREGATE_REQUEST:
d1e2cf21 1605 ofp_print_stats_request(string, oh);
2af0c0ef 1606 ofp_print_flow_stats_request(string, oh);
d1e2cf21
BP
1607 break;
1608
1609 case OFPUTIL_OFPST_TABLE_REQUEST:
1610 ofp_print_stats_request(string, oh);
1611 break;
1612
1613 case OFPUTIL_OFPST_PORT_REQUEST:
1614 ofp_print_stats_request(string, oh);
1615 ofp_print_ofpst_port_request(string, oh);
1616 break;
1617
1618 case OFPUTIL_OFPST_QUEUE_REQUEST:
1619 ofp_print_stats_request(string, oh);
1620 ofp_print_ofpst_queue_request(string, oh);
1621 break;
1622
1623 case OFPUTIL_OFPST_DESC_REPLY:
1624 ofp_print_stats_reply(string, oh);
1625 ofp_print_ofpst_desc_reply(string, oh);
1626 break;
1627
1628 case OFPUTIL_OFPST_FLOW_REPLY:
1629 ofp_print_stats_reply(string, oh);
1630 ofp_print_ofpst_flow_reply(string, oh, verbosity);
1631 break;
1632
1633 case OFPUTIL_OFPST_QUEUE_REPLY:
1634 ofp_print_stats_reply(string, oh);
1635 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1636 break;
1637
1638 case OFPUTIL_OFPST_PORT_REPLY:
1639 ofp_print_stats_reply(string, oh);
1640 ofp_print_ofpst_port_reply(string, oh, verbosity);
1641 break;
1642
1643 case OFPUTIL_OFPST_TABLE_REPLY:
1644 ofp_print_stats_reply(string, oh);
1645 ofp_print_ofpst_table_reply(string, oh, verbosity);
1646 break;
1647
1648 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1649 ofp_print_stats_reply(string, oh);
1650 ofp_print_ofpst_aggregate_reply(string, oh);
1651 break;
064af421 1652
d1e2cf21
BP
1653 case OFPUTIL_NXT_STATUS_REQUEST:
1654 case OFPUTIL_NXT_STATUS_REPLY:
3731490f 1655 ofp_print_nxt_status_message(string, oh);
7fa91113
BP
1656 break;
1657
d1e2cf21 1658 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
7fa91113
BP
1659 ofp_print_nxt_tun_id_from_cookie(string, msg);
1660 break;
1661
d1e2cf21
BP
1662 case OFPUTIL_NXT_ROLE_REQUEST:
1663 case OFPUTIL_NXT_ROLE_REPLY:
61fe3a7b 1664 ofp_print_nxt_role_message(string, msg);
7fa91113
BP
1665 break;
1666
d1e2cf21 1667 case OFPUTIL_NXT_SET_FLOW_FORMAT:
7fa91113
BP
1668 ofp_print_nxt_set_flow_format(string, msg);
1669 break;
1670
d1e2cf21 1671 case OFPUTIL_NXT_FLOW_MOD:
7fa91113
BP
1672 ofp_print_flow_mod(string, msg, code, verbosity);
1673 break;
1674
d1e2cf21 1675 case OFPUTIL_NXST_FLOW_REPLY:
c6430da5
BP
1676 ofp_print_nxst_flow_reply(string, oh);
1677 break;
1678
d1e2cf21 1679 case OFPUTIL_NXST_AGGREGATE_REPLY:
a2ad9ecd 1680 ofp_print_stats_reply(string, oh);
9b045a0c 1681 ofp_print_nxst_aggregate_reply(string, msg);
d1e2cf21 1682 break;
246e61ea 1683 }
d1e2cf21 1684}
064af421
BP
1685
1686/* Composes and returns a string representing the OpenFlow packet of 'len'
1687 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1688 * verbosity and higher numbers increase verbosity. The caller is responsible
1689 * for freeing the string. */
1690char *
1691ofp_to_string(const void *oh_, size_t len, int verbosity)
1692{
1693 struct ds string = DS_EMPTY_INITIALIZER;
1694 const struct ofp_header *oh = oh_;
064af421
BP
1695
1696 if (len < sizeof(struct ofp_header)) {
1697 ds_put_cstr(&string, "OpenFlow packet too short:\n");
064af421 1698 } else if (oh->version != OFP_VERSION) {
d1e2cf21
BP
1699 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1700 oh->version);
1701 } else if (ntohs(oh->length) > len) {
1702 ds_put_format(&string,
1703 "(***truncated to %zu bytes from %"PRIu16"***)",
1704 len, ntohs(oh->length));
1705 } else if (ntohs(oh->length) < len) {
1706 ds_put_format(&string,
1707 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1708 ntohs(oh->length), len);
1709 } else {
1710 const struct ofputil_msg_type *type;
d1e2cf21
BP
1711 int error;
1712
1713 error = ofputil_decode_msg_type(oh, &type);
1714 if (!error) {
1715 ofp_to_string__(oh, type, &string, verbosity);
7fa91113
BP
1716 if (verbosity >= 5) {
1717 if (ds_last(&string) != '\n') {
1718 ds_put_char(&string, '\n');
1719 }
d1e2cf21
BP
1720 ds_put_hex_dump(&string, oh, len, 0, true);
1721 }
7fa91113 1722 if (ds_last(&string) != '\n') {
d1e2cf21
BP
1723 ds_put_char(&string, '\n');
1724 }
1725 return ds_steal_cstr(&string);
064af421 1726 }
064af421 1727
7fa91113 1728 ofp_print_error(&string, error);
064af421 1729 }
d1e2cf21
BP
1730 ds_put_hex_dump(&string, oh, len, 0, true);
1731 return ds_steal_cstr(&string);
064af421
BP
1732}
1733
1734/* Returns the name for the specified OpenFlow message type as a string,
1735 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1736 * hex number, e.g. "0x55".
1737 *
1738 * The caller must free the returned string when it is no longer needed. */
1739char *
1740ofp_message_type_to_string(uint8_t type)
1741{
d1e2cf21 1742 const char *name;
064af421 1743
d1e2cf21
BP
1744 switch (type) {
1745 case OFPT_HELLO:
1746 name = "HELLO";
1747 break;
1748 case OFPT_ERROR:
1749 name = "ERROR";
1750 break;
1751 case OFPT_ECHO_REQUEST:
1752 name = "ECHO_REQUEST";
1753 break;
1754 case OFPT_ECHO_REPLY:
1755 name = "ECHO_REPLY";
1756 break;
1757 case OFPT_VENDOR:
1758 name = "VENDOR";
1759 break;
1760 case OFPT_FEATURES_REQUEST:
1761 name = "FEATURES_REQUEST";
1762 break;
1763 case OFPT_FEATURES_REPLY:
1764 name = "FEATURES_REPLY";
1765 break;
1766 case OFPT_GET_CONFIG_REQUEST:
1767 name = "GET_CONFIG_REQUEST";
1768 break;
1769 case OFPT_GET_CONFIG_REPLY:
1770 name = "GET_CONFIG_REPLY";
1771 break;
1772 case OFPT_SET_CONFIG:
1773 name = "SET_CONFIG";
1774 break;
1775 case OFPT_PACKET_IN:
1776 name = "PACKET_IN";
1777 break;
1778 case OFPT_FLOW_REMOVED:
1779 name = "FLOW_REMOVED";
1780 break;
1781 case OFPT_PORT_STATUS:
1782 name = "PORT_STATUS";
1783 break;
1784 case OFPT_PACKET_OUT:
1785 name = "PACKET_OUT";
1786 break;
1787 case OFPT_FLOW_MOD:
1788 name = "FLOW_MOD";
1789 break;
1790 case OFPT_PORT_MOD:
1791 name = "PORT_MOD";
1792 break;
1793 case OFPT_STATS_REQUEST:
1794 name = "STATS_REQUEST";
1795 break;
1796 case OFPT_STATS_REPLY:
1797 name = "STATS_REPLY";
1798 break;
1799 case OFPT_BARRIER_REQUEST:
1800 name = "BARRIER_REQUEST";
1801 break;
1802 case OFPT_BARRIER_REPLY:
1803 name = "BARRIER_REPLY";
1804 break;
1805 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1806 name = "QUEUE_GET_CONFIG_REQUEST";
1807 break;
1808 case OFPT_QUEUE_GET_CONFIG_REPLY:
1809 name = "QUEUE_GET_CONFIG_REPLY";
1810 break;
1811 default:
1812 name = NULL;
1813 break;
064af421 1814 }
d1e2cf21
BP
1815
1816 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
064af421
BP
1817}
1818\f
1819static void
d295e8e9 1820print_and_free(FILE *stream, char *string)
064af421
BP
1821{
1822 fputs(string, stream);
1823 free(string);
1824}
1825
1826/* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1827 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1828 * numbers increase verbosity. */
1829void
1830ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1831{
1832 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1833}
1834
1835/* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1836 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1837 * the Ethernet frame (of which 'len' bytes were captured).
1838 *
1839 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1840void
1841ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1842{
1843 print_and_free(stream, ofp_packet_to_string(data, len, total_len));
1844}