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