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