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