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