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