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