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