2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ofp-print.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
30 #include "byte-order.h"
32 #include "dynamic-string.h"
35 #include "multipath.h"
36 #include "meta-flow.h"
39 #include "ofp-errors.h"
42 #include "openflow/openflow.h"
43 #include "openflow/nicira-ext.h"
46 #include "type-props.h"
47 #include "unaligned.h"
50 static void ofp_print_queue_name(struct ds *string, uint32_t port);
51 static void ofp_print_error(struct ds *, enum ofperr);
54 /* Returns a string that represents the contents of the Ethernet frame in the
55 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
57 ofp_packet_to_string(const void *data, size_t len)
59 struct ds ds = DS_EMPTY_INITIALIZER;
63 ofpbuf_use_const(&buf, data, len);
64 flow_extract(&buf, 0, 0, 0, &flow);
65 flow_format(&ds, &flow);
68 if (flow.nw_proto == IPPROTO_TCP) {
69 struct tcp_header *th = buf.l4;
70 ds_put_format(&ds, " tcp_csum:%"PRIx16,
72 } else if (flow.nw_proto == IPPROTO_UDP) {
73 struct udp_header *uh = buf.l4;
74 ds_put_format(&ds, " udp_csum:%"PRIx16,
79 ds_put_char(&ds, '\n');
85 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
88 struct ofputil_packet_in pin;
92 error = ofputil_decode_packet_in(&pin, oh);
94 ofp_print_error(string, error);
99 ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
103 ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
106 ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
107 ofputil_format_port(pin.fmd.in_port, string);
109 if (pin.fmd.tun_id_mask) {
110 ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
111 if (pin.fmd.tun_id_mask != htonll(UINT64_MAX)) {
112 ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.tun_id_mask));
116 for (i = 0; i < FLOW_N_REGS; i++) {
117 if (pin.fmd.reg_masks[i]) {
118 ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
119 if (pin.fmd.reg_masks[i] != UINT32_MAX) {
120 ds_put_format(string, "/0x%"PRIx32, pin.fmd.reg_masks[i]);
125 ds_put_format(string, " (via %s)",
126 ofputil_packet_in_reason_to_string(pin.reason));
128 ds_put_format(string, " data_len=%zu", pin.packet_len);
129 if (pin.buffer_id == UINT32_MAX) {
130 ds_put_format(string, " (unbuffered)");
131 if (pin.total_len != pin.packet_len) {
132 ds_put_format(string, " (***total_len != data_len***)");
135 ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
136 if (pin.total_len < pin.packet_len) {
137 ds_put_format(string, " (***total_len < data_len***)");
140 ds_put_char(string, '\n');
143 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
144 ds_put_cstr(string, packet);
150 print_note(struct ds *string, const struct nx_action_note *nan)
155 ds_put_cstr(string, "note:");
156 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
157 for (i = 0; i < len; i++) {
159 ds_put_char(string, '.');
161 ds_put_format(string, "%02"PRIx8, nan->note[i]);
166 ofp_print_action(struct ds *s, const union ofp_action *a,
167 enum ofputil_action_code code)
169 const struct ofp_action_enqueue *oae;
170 const struct ofp_action_dl_addr *oada;
171 const struct nx_action_set_tunnel64 *nast64;
172 const struct nx_action_set_tunnel *nast;
173 const struct nx_action_set_queue *nasq;
174 const struct nx_action_resubmit *nar;
175 const struct nx_action_reg_move *move;
176 const struct nx_action_reg_load *load;
177 const struct nx_action_multipath *nam;
178 const struct nx_action_autopath *naa;
179 const struct nx_action_output_reg *naor;
180 const struct nx_action_fin_timeout *naft;
181 const struct nx_action_controller *nac;
182 struct mf_subfield subfield;
186 case OFPUTIL_OFPAT10_OUTPUT:
187 port = ntohs(a->output.port);
188 if (port < OFPP_MAX) {
189 ds_put_format(s, "output:%"PRIu16, port);
191 ofputil_format_port(port, s);
192 if (port == OFPP_CONTROLLER) {
193 if (a->output.max_len != htons(0)) {
194 ds_put_format(s, ":%"PRIu16, ntohs(a->output.max_len));
196 ds_put_cstr(s, ":all");
202 case OFPUTIL_OFPAT10_ENQUEUE:
203 oae = (const struct ofp_action_enqueue *) a;
204 ds_put_format(s, "enqueue:");
205 ofputil_format_port(ntohs(oae->port), s);
206 ds_put_format(s, "q%"PRIu32, ntohl(oae->queue_id));
209 case OFPUTIL_OFPAT10_SET_VLAN_VID:
210 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
211 ntohs(a->vlan_vid.vlan_vid));
214 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
215 ds_put_format(s, "mod_vlan_pcp:%"PRIu8, a->vlan_pcp.vlan_pcp);
218 case OFPUTIL_OFPAT10_STRIP_VLAN:
219 ds_put_cstr(s, "strip_vlan");
222 case OFPUTIL_OFPAT10_SET_DL_SRC:
223 oada = (const struct ofp_action_dl_addr *) a;
224 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
225 ETH_ADDR_ARGS(oada->dl_addr));
228 case OFPUTIL_OFPAT10_SET_DL_DST:
229 oada = (const struct ofp_action_dl_addr *) a;
230 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
231 ETH_ADDR_ARGS(oada->dl_addr));
234 case OFPUTIL_OFPAT10_SET_NW_SRC:
235 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
238 case OFPUTIL_OFPAT10_SET_NW_DST:
239 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
242 case OFPUTIL_OFPAT10_SET_NW_TOS:
243 ds_put_format(s, "mod_nw_tos:%d", a->nw_tos.nw_tos);
246 case OFPUTIL_OFPAT10_SET_TP_SRC:
247 ds_put_format(s, "mod_tp_src:%d", ntohs(a->tp_port.tp_port));
250 case OFPUTIL_OFPAT10_SET_TP_DST:
251 ds_put_format(s, "mod_tp_dst:%d", ntohs(a->tp_port.tp_port));
254 case OFPUTIL_NXAST_RESUBMIT:
255 nar = (struct nx_action_resubmit *)a;
256 ds_put_format(s, "resubmit:");
257 ofputil_format_port(ntohs(nar->in_port), s);
260 case OFPUTIL_NXAST_RESUBMIT_TABLE:
261 nar = (struct nx_action_resubmit *)a;
262 ds_put_format(s, "resubmit(");
263 if (nar->in_port != htons(OFPP_IN_PORT)) {
264 ofputil_format_port(ntohs(nar->in_port), s);
267 if (nar->table != 255) {
268 ds_put_format(s, "%"PRIu8, nar->table);
273 case OFPUTIL_NXAST_SET_TUNNEL:
274 nast = (struct nx_action_set_tunnel *)a;
275 ds_put_format(s, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
278 case OFPUTIL_NXAST_SET_QUEUE:
279 nasq = (struct nx_action_set_queue *)a;
280 ds_put_format(s, "set_queue:%u", ntohl(nasq->queue_id));
283 case OFPUTIL_NXAST_POP_QUEUE:
284 ds_put_cstr(s, "pop_queue");
287 case OFPUTIL_NXAST_NOTE:
288 print_note(s, (const struct nx_action_note *) a);
291 case OFPUTIL_NXAST_REG_MOVE:
292 move = (const struct nx_action_reg_move *) a;
293 nxm_format_reg_move(move, s);
296 case OFPUTIL_NXAST_REG_LOAD:
297 load = (const struct nx_action_reg_load *) a;
298 nxm_format_reg_load(load, s);
301 case OFPUTIL_NXAST_SET_TUNNEL64:
302 nast64 = (const struct nx_action_set_tunnel64 *) a;
303 ds_put_format(s, "set_tunnel64:%#"PRIx64,
304 ntohll(nast64->tun_id));
307 case OFPUTIL_NXAST_MULTIPATH:
308 nam = (const struct nx_action_multipath *) a;
309 multipath_format(nam, s);
312 case OFPUTIL_NXAST_AUTOPATH:
313 naa = (const struct nx_action_autopath *)a;
314 ds_put_format(s, "autopath(%u,", ntohl(naa->id));
315 nxm_decode(&subfield, naa->dst, naa->ofs_nbits);
316 mf_format_subfield(&subfield, s);
320 case OFPUTIL_NXAST_BUNDLE:
321 case OFPUTIL_NXAST_BUNDLE_LOAD:
322 bundle_format((const struct nx_action_bundle *) a, s);
325 case OFPUTIL_NXAST_OUTPUT_REG:
326 naor = (const struct nx_action_output_reg *) a;
327 ds_put_cstr(s, "output:");
328 nxm_decode(&subfield, naor->src, naor->ofs_nbits);
329 mf_format_subfield(&subfield, s);
332 case OFPUTIL_NXAST_LEARN:
333 learn_format((const struct nx_action_learn *) a, s);
336 case OFPUTIL_NXAST_DEC_TTL:
337 ds_put_cstr(s, "dec_ttl");
340 case OFPUTIL_NXAST_EXIT:
341 ds_put_cstr(s, "exit");
344 case OFPUTIL_NXAST_FIN_TIMEOUT:
345 naft = (const struct nx_action_fin_timeout *) a;
346 ds_put_cstr(s, "fin_timeout(");
347 if (naft->fin_idle_timeout) {
348 ds_put_format(s, "idle_timeout=%"PRIu16",",
349 ntohs(naft->fin_idle_timeout));
351 if (naft->fin_hard_timeout) {
352 ds_put_format(s, "hard_timeout=%"PRIu16",",
353 ntohs(naft->fin_hard_timeout));
359 case OFPUTIL_NXAST_CONTROLLER:
360 nac = (const struct nx_action_controller *) a;
361 ds_put_cstr(s, "controller(");
362 if (nac->reason != OFPR_ACTION) {
363 ds_put_format(s, "reason=%s,",
364 ofputil_packet_in_reason_to_string(nac->reason));
366 if (nac->max_len != htons(UINT16_MAX)) {
367 ds_put_format(s, "max_len=%"PRIu16",", ntohs(nac->max_len));
369 if (nac->controller_id != htons(0)) {
370 ds_put_format(s, "id=%"PRIu16",", ntohs(nac->controller_id));
382 ofp_print_actions(struct ds *string, const union ofp_action *actions,
385 const union ofp_action *a;
388 ds_put_cstr(string, "actions=");
390 ds_put_cstr(string, "drop");
393 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
394 int code = ofputil_decode_action(a);
397 ds_put_cstr(string, ",");
399 ofp_print_action(string, a, code);
401 ofp_print_error(string, -code);
405 ds_put_format(string, " ***%zu leftover bytes following actions",
411 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
414 struct ofputil_packet_out po;
417 error = ofputil_decode_packet_out(&po, opo);
419 ofp_print_error(string, error);
423 ds_put_cstr(string, " in_port=");
424 ofputil_format_port(po.in_port, string);
426 ds_put_char(string, ' ');
427 ofp_print_actions(string, po.actions, po.n_actions);
429 if (po.buffer_id == UINT32_MAX) {
430 ds_put_format(string, " data_len=%zu", po.packet_len);
431 if (verbosity > 0 && po.packet_len > 0) {
432 char *packet = ofp_packet_to_string(po.packet, po.packet_len);
433 ds_put_char(string, '\n');
434 ds_put_cstr(string, packet);
438 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
440 ds_put_char(string, '\n');
443 /* qsort comparison function. */
445 compare_ports(const void *a_, const void *b_)
447 const struct ofputil_phy_port *a = a_;
448 const struct ofputil_phy_port *b = b_;
449 uint16_t ap = a->port_no;
450 uint16_t bp = b->port_no;
452 return ap < bp ? -1 : ap > bp;
456 ofp_print_bit_names(struct ds *string, uint32_t bits,
457 const char *(*bit_to_name)(uint32_t bit))
463 ds_put_cstr(string, "0");
467 for (i = 0; i < 32; i++) {
468 uint32_t bit = UINT32_C(1) << i;
471 const char *name = bit_to_name(bit);
474 ds_put_char(string, ' ');
476 ds_put_cstr(string, name);
484 ds_put_char(string, ' ');
486 ds_put_format(string, "0x%"PRIx32, bits);
491 netdev_feature_to_name(uint32_t bit)
493 enum netdev_features f = bit;
496 case NETDEV_F_10MB_HD: return "10MB-HD";
497 case NETDEV_F_10MB_FD: return "10MB-FD";
498 case NETDEV_F_100MB_HD: return "100MB-HD";
499 case NETDEV_F_100MB_FD: return "100MB-FD";
500 case NETDEV_F_1GB_HD: return "1GB-HD";
501 case NETDEV_F_1GB_FD: return "1GB-FD";
502 case NETDEV_F_10GB_FD: return "10GB-FD";
503 case NETDEV_F_40GB_FD: return "40GB-FD";
504 case NETDEV_F_100GB_FD: return "100GB-FD";
505 case NETDEV_F_1TB_FD: return "1TB-FD";
506 case NETDEV_F_OTHER: return "OTHER";
507 case NETDEV_F_COPPER: return "COPPER";
508 case NETDEV_F_FIBER: return "FIBER";
509 case NETDEV_F_AUTONEG: return "AUTO_NEG";
510 case NETDEV_F_PAUSE: return "AUTO_PAUSE";
511 case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
518 ofp_print_port_features(struct ds *string, enum netdev_features features)
520 ofp_print_bit_names(string, features, netdev_feature_to_name);
521 ds_put_char(string, '\n');
525 ofputil_port_config_to_name(uint32_t bit)
527 enum ofputil_port_config pc = bit;
530 case OFPUTIL_PC_PORT_DOWN: return "PORT_DOWN";
531 case OFPUTIL_PC_NO_STP: return "NO_STP";
532 case OFPUTIL_PC_NO_RECV: return "NO_RECV";
533 case OFPUTIL_PC_NO_RECV_STP: return "NO_RECV_STP";
534 case OFPUTIL_PC_NO_FLOOD: return "NO_FLOOD";
535 case OFPUTIL_PC_NO_FWD: return "NO_FWD";
536 case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
543 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
545 ofp_print_bit_names(string, config, ofputil_port_config_to_name);
546 ds_put_char(string, '\n');
550 ofputil_port_state_to_name(uint32_t bit)
552 enum ofputil_port_state ps = bit;
555 case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
556 case OFPUTIL_PS_BLOCKED: return "BLOCKED";
557 case OFPUTIL_PS_LIVE: return "LIVE";
559 case OFPUTIL_PS_STP_LISTEN:
560 case OFPUTIL_PS_STP_LEARN:
561 case OFPUTIL_PS_STP_FORWARD:
562 case OFPUTIL_PS_STP_BLOCK:
563 /* Handled elsewhere. */
571 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
573 enum ofputil_port_state stp_state;
575 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
576 * pattern. We have to special case it.
578 * OVS doesn't support STP, so this field will always be 0 if we are
579 * talking to OVS, so we'd always print STP_LISTEN in that case.
580 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
581 * avoid confusing users. */
582 stp_state = state & OFPUTIL_PS_STP_MASK;
585 (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
586 : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
588 state &= ~OFPUTIL_PS_STP_MASK;
590 ofp_print_bit_names(string, state, ofputil_port_state_to_name);
593 ofp_print_bit_names(string, state, ofputil_port_state_to_name);
595 ds_put_char(string, '\n');
599 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
601 char name[sizeof port->name];
604 memcpy(name, port->name, sizeof name);
605 for (j = 0; j < sizeof name - 1; j++) {
606 if (!isprint((unsigned char) name[j])) {
612 ds_put_char(string, ' ');
613 ofputil_format_port(port->port_no, string);
614 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
615 name, ETH_ADDR_ARGS(port->hw_addr));
617 ds_put_cstr(string, " config: ");
618 ofp_print_port_config(string, port->config);
620 ds_put_cstr(string, " state: ");
621 ofp_print_port_state(string, port->state);
624 ds_put_format(string, " current: ");
625 ofp_print_port_features(string, port->curr);
627 if (port->advertised) {
628 ds_put_format(string, " advertised: ");
629 ofp_print_port_features(string, port->advertised);
631 if (port->supported) {
632 ds_put_format(string, " supported: ");
633 ofp_print_port_features(string, port->supported);
636 ds_put_format(string, " peer: ");
637 ofp_print_port_features(string, port->peer);
639 ds_put_format(string, " speed: %"PRIu32" Mbps now, "
640 "%"PRIu32" Mbps max\n",
641 port->curr_speed / UINT32_C(1000),
642 port->max_speed / UINT32_C(1000));
646 ofputil_capabilities_to_name(uint32_t bit)
648 enum ofputil_capabilities capabilities = bit;
650 switch (capabilities) {
651 case OFPUTIL_C_FLOW_STATS: return "FLOW_STATS";
652 case OFPUTIL_C_TABLE_STATS: return "TABLE_STATS";
653 case OFPUTIL_C_PORT_STATS: return "PORT_STATS";
654 case OFPUTIL_C_IP_REASM: return "IP_REASM";
655 case OFPUTIL_C_QUEUE_STATS: return "QUEUE_STATS";
656 case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
657 case OFPUTIL_C_STP: return "STP";
658 case OFPUTIL_C_GROUP_STATS: return "GROUP_STATS";
665 ofputil_action_bitmap_to_name(uint32_t bit)
667 enum ofputil_action_bitmap action = bit;
670 case OFPUTIL_A_OUTPUT: return "OUTPUT";
671 case OFPUTIL_A_SET_VLAN_VID: return "SET_VLAN_VID";
672 case OFPUTIL_A_SET_VLAN_PCP: return "SET_VLAN_PCP";
673 case OFPUTIL_A_STRIP_VLAN: return "STRIP_VLAN";
674 case OFPUTIL_A_SET_DL_SRC: return "SET_DL_SRC";
675 case OFPUTIL_A_SET_DL_DST: return "SET_DL_DST";
676 case OFPUTIL_A_SET_NW_SRC: return "SET_NW_SRC";
677 case OFPUTIL_A_SET_NW_DST: return "SET_NW_DST";
678 case OFPUTIL_A_SET_NW_ECN: return "SET_NW_ECN";
679 case OFPUTIL_A_SET_NW_TOS: return "SET_NW_TOS";
680 case OFPUTIL_A_SET_TP_SRC: return "SET_TP_SRC";
681 case OFPUTIL_A_SET_TP_DST: return "SET_TP_DST";
682 case OFPUTIL_A_ENQUEUE: return "ENQUEUE";
683 case OFPUTIL_A_COPY_TTL_OUT: return "COPY_TTL_OUT";
684 case OFPUTIL_A_COPY_TTL_IN: return "COPY_TTL_IN";
685 case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
686 case OFPUTIL_A_SET_MPLS_TC: return "SET_MPLS_TC";
687 case OFPUTIL_A_SET_MPLS_TTL: return "SET_MPLS_TTL";
688 case OFPUTIL_A_DEC_MPLS_TTL: return "DEC_MPLS_TTL";
689 case OFPUTIL_A_PUSH_VLAN: return "PUSH_VLAN";
690 case OFPUTIL_A_POP_VLAN: return "POP_VLAN";
691 case OFPUTIL_A_PUSH_MPLS: return "PUSH_MPLS";
692 case OFPUTIL_A_POP_MPLS: return "POP_MPLS";
693 case OFPUTIL_A_SET_QUEUE: return "SET_QUEUE";
694 case OFPUTIL_A_GROUP: return "GROUP";
695 case OFPUTIL_A_SET_NW_TTL: return "SET_NW_TTL";
696 case OFPUTIL_A_DEC_NW_TTL: return "DEC_NW_TTL";
703 ofp_print_switch_features(struct ds *string,
704 const struct ofp_switch_features *osf)
706 struct ofputil_switch_features features;
707 struct ofputil_phy_port *ports;
713 error = ofputil_decode_switch_features(osf, &features, &b);
715 ofp_print_error(string, error);
719 ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
720 ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32"\n",
721 features.n_tables, features.n_buffers);
723 ds_put_cstr(string, "capabilities: ");
724 ofp_print_bit_names(string, features.capabilities,
725 ofputil_capabilities_to_name);
726 ds_put_char(string, '\n');
728 ds_put_cstr(string, "actions: ");
729 ofp_print_bit_names(string, features.actions,
730 ofputil_action_bitmap_to_name);
731 ds_put_char(string, '\n');
733 n_ports = ofputil_count_phy_ports(osf);
735 ports = xmalloc(n_ports * sizeof *ports);
736 for (i = 0; i < n_ports; i++) {
737 error = ofputil_pull_switch_features_port(&b, &ports[i]);
739 ofp_print_error(string, error);
743 qsort(ports, n_ports, sizeof *ports, compare_ports);
744 for (i = 0; i < n_ports; i++) {
745 ofp_print_phy_port(string, &ports[i]);
751 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
753 enum ofp_config_flags flags;
755 flags = ntohs(osc->flags);
757 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
758 flags &= ~OFPC_FRAG_MASK;
760 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
761 ds_put_format(string, " invalid_ttl_to_controller");
762 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
766 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
769 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
772 static void print_wild(struct ds *string, const char *leader, int is_wild,
773 int verbosity, const char *format, ...)
774 __attribute__((format(printf, 5, 6)));
776 static void print_wild(struct ds *string, const char *leader, int is_wild,
777 int verbosity, const char *format, ...)
779 if (is_wild && verbosity < 2) {
782 ds_put_cstr(string, leader);
786 va_start(args, format);
787 ds_put_format_valist(string, format, args);
790 ds_put_char(string, '*');
792 ds_put_char(string, ',');
796 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
797 uint32_t wild_bits, int verbosity)
799 if (wild_bits >= 32 && verbosity < 2) {
802 ds_put_cstr(string, leader);
803 if (wild_bits < 32) {
804 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
806 ds_put_format(string, "/%d", 32 - wild_bits);
809 ds_put_char(string, '*');
811 ds_put_char(string, ',');
815 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
817 char *s = ofp_match_to_string(om, verbosity);
823 ofp_match_to_string(const struct ofp_match *om, int verbosity)
825 struct ds f = DS_EMPTY_INITIALIZER;
826 uint32_t w = ntohl(om->wildcards);
827 bool skip_type = false;
828 bool skip_proto = false;
830 if (!(w & OFPFW_DL_TYPE)) {
832 if (om->dl_type == htons(ETH_TYPE_IP)) {
833 if (!(w & OFPFW_NW_PROTO)) {
835 if (om->nw_proto == IPPROTO_ICMP) {
836 ds_put_cstr(&f, "icmp,");
837 } else if (om->nw_proto == IPPROTO_TCP) {
838 ds_put_cstr(&f, "tcp,");
839 } else if (om->nw_proto == IPPROTO_UDP) {
840 ds_put_cstr(&f, "udp,");
842 ds_put_cstr(&f, "ip,");
846 ds_put_cstr(&f, "ip,");
848 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
849 ds_put_cstr(&f, "arp,");
854 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
855 "%d", ntohs(om->in_port));
856 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
857 "%d", ntohs(om->dl_vlan));
858 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
859 "%d", om->dl_vlan_pcp);
860 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
861 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
862 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
863 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
865 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
866 "0x%04x", ntohs(om->dl_type));
868 print_ip_netmask(&f, "nw_src=", om->nw_src,
869 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
870 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
871 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
873 if (om->dl_type == htons(ETH_TYPE_ARP)) {
874 print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
877 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
881 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
883 if (om->nw_proto == IPPROTO_ICMP) {
884 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
885 "%d", ntohs(om->tp_src));
886 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
887 "%d", ntohs(om->tp_dst));
889 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
890 "%d", ntohs(om->tp_src));
891 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
892 "%d", ntohs(om->tp_dst));
894 if (ds_last(&f) == ',') {
901 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
902 enum ofputil_msg_code code, int verbosity)
904 struct ofputil_flow_mod fm;
908 error = ofputil_decode_flow_mod(&fm, oh, OFPUTIL_P_OF10_TID);
910 ofp_print_error(s, error);
915 switch (fm.command) {
917 ds_put_cstr(s, "ADD");
920 ds_put_cstr(s, "MOD");
922 case OFPFC_MODIFY_STRICT:
923 ds_put_cstr(s, "MOD_STRICT");
926 ds_put_cstr(s, "DEL");
928 case OFPFC_DELETE_STRICT:
929 ds_put_cstr(s, "DEL_STRICT");
932 ds_put_format(s, "cmd:%d", fm.command);
934 if (fm.table_id != 0) {
935 ds_put_format(s, " table:%d", fm.table_id);
939 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
940 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
941 ofp_print_match(s, &ofm->match, verbosity);
943 /* ofp_print_match() doesn't print priority. */
944 need_priority = true;
945 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
946 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
947 const void *nxm = nfm + 1;
950 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
951 ds_put_cstr(s, nxm_s);
954 /* nx_match_to_string() doesn't print priority. */
955 need_priority = true;
957 cls_rule_format(&fm.cr, s);
959 /* cls_rule_format() does print priority. */
960 need_priority = false;
963 if (ds_last(s) != ' ') {
966 if (fm.cookie != htonll(0)) {
967 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
969 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
970 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
972 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
973 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
975 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
976 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
978 if (fm.buffer_id != UINT32_MAX) {
979 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
982 uint16_t flags = fm.flags;
984 if (flags & OFPFF_SEND_FLOW_REM) {
985 ds_put_cstr(s, "send_flow_rem ");
987 if (flags & OFPFF_CHECK_OVERLAP) {
988 ds_put_cstr(s, "check_overlap ");
990 if (flags & OFPFF_EMERG) {
991 ds_put_cstr(s, "emerg ");
994 flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP | OFPFF_EMERG);
996 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
1000 ofp_print_actions(s, fm.actions, fm.n_actions);
1004 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
1006 ds_put_format(string, "%u", sec);
1008 ds_put_format(string, ".%09u", nsec);
1009 while (string->string[string->length - 1] == '0') {
1013 ds_put_char(string, 's');
1017 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
1022 case OFPRR_IDLE_TIMEOUT:
1024 case OFPRR_HARD_TIMEOUT:
1028 case OFPRR_GROUP_DELETE:
1029 return "group_delete";
1031 sprintf(s, "%d", (int) reason);
1037 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
1039 struct ofputil_flow_removed fr;
1042 error = ofputil_decode_flow_removed(&fr, oh);
1044 ofp_print_error(string, error);
1048 ds_put_char(string, ' ');
1049 cls_rule_format(&fr.rule, string);
1051 ds_put_format(string, " reason=%s",
1052 ofp_flow_removed_reason_to_string(fr.reason));
1054 if (fr.cookie != htonll(0)) {
1055 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
1057 ds_put_cstr(string, " duration");
1058 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
1059 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
1060 fr.idle_timeout, fr.packet_count, fr.byte_count);
1064 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
1066 struct ofputil_port_mod pm;
1069 error = ofputil_decode_port_mod(oh, &pm);
1071 ofp_print_error(string, error);
1075 ds_put_format(string, "port: %"PRIu16": addr:"ETH_ADDR_FMT"\n",
1076 pm.port_no, ETH_ADDR_ARGS(pm.hw_addr));
1078 ds_put_format(string, " config: ");
1079 ofp_print_port_config(string, pm.config);
1081 ds_put_format(string, " mask: ");
1082 ofp_print_port_config(string, pm.mask);
1084 ds_put_format(string, " advertise: ");
1086 ofp_print_port_features(string, pm.advertise);
1088 ds_put_format(string, "UNCHANGED\n");
1093 ofp_print_error(struct ds *string, enum ofperr error)
1095 if (string->length) {
1096 ds_put_char(string, ' ');
1098 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1102 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1104 size_t len = ntohs(oem->header.length);
1105 size_t payload_ofs, payload_len;
1106 const void *payload;
1110 error = ofperr_decode_msg(&oem->header, &payload_ofs);
1112 ds_put_cstr(string, "***decode error***");
1113 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1117 ds_put_format(string, " %s\n", ofperr_get_name(error));
1119 payload = (const uint8_t *) oem + payload_ofs;
1120 payload_len = len - payload_ofs;
1121 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1122 ds_put_printable(string, payload, payload_len);
1124 s = ofp_to_string(payload, payload_len, 1);
1125 ds_put_cstr(string, s);
1131 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1133 struct ofputil_port_status ps;
1136 error = ofputil_decode_port_status(ops, &ps);
1138 ofp_print_error(string, error);
1142 if (ps.reason == OFPPR_ADD) {
1143 ds_put_format(string, " ADD:");
1144 } else if (ps.reason == OFPPR_DELETE) {
1145 ds_put_format(string, " DEL:");
1146 } else if (ps.reason == OFPPR_MODIFY) {
1147 ds_put_format(string, " MOD:");
1150 ofp_print_phy_port(string, &ps.desc);
1154 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
1156 ds_put_char(string, '\n');
1157 ds_put_format(string, "Manufacturer: %.*s\n",
1158 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1159 ds_put_format(string, "Hardware: %.*s\n",
1160 (int) sizeof ods->hw_desc, ods->hw_desc);
1161 ds_put_format(string, "Software: %.*s\n",
1162 (int) sizeof ods->sw_desc, ods->sw_desc);
1163 ds_put_format(string, "Serial Num: %.*s\n",
1164 (int) sizeof ods->serial_num, ods->serial_num);
1165 ds_put_format(string, "DP Description: %.*s\n",
1166 (int) sizeof ods->dp_desc, ods->dp_desc);
1170 ofp_print_flow_stats_request(struct ds *string,
1171 const struct ofp_stats_msg *osm)
1173 struct ofputil_flow_stats_request fsr;
1176 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
1178 ofp_print_error(string, error);
1182 if (fsr.table_id != 0xff) {
1183 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1186 if (fsr.out_port != OFPP_NONE) {
1187 ds_put_cstr(string, " out_port=");
1188 ofputil_format_port(fsr.out_port, string);
1191 /* A flow stats request doesn't include a priority, but cls_rule_format()
1192 * will print one unless it is OFP_DEFAULT_PRIORITY. */
1193 fsr.match.priority = OFP_DEFAULT_PRIORITY;
1195 ds_put_char(string, ' ');
1196 cls_rule_format(&fsr.match, string);
1200 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1204 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1206 struct ofputil_flow_stats fs;
1209 retval = ofputil_decode_flow_stats_reply(&fs, &b, true);
1211 if (retval != EOF) {
1212 ds_put_cstr(string, " ***parse error***");
1217 ds_put_char(string, '\n');
1219 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1221 ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
1222 ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
1223 ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
1224 ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
1225 if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
1226 ds_put_format(string, "idle_timeout=%"PRIu16", ", fs.idle_timeout);
1228 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1229 ds_put_format(string, "hard_timeout=%"PRIu16", ", fs.hard_timeout);
1231 if (fs.idle_age >= 0) {
1232 ds_put_format(string, "idle_age=%d, ", fs.idle_age);
1234 if (fs.hard_age >= 0 && fs.hard_age != fs.duration_sec) {
1235 ds_put_format(string, "hard_age=%d, ", fs.hard_age);
1238 cls_rule_format(&fs.rule, string);
1239 if (string->string[string->length - 1] != ' ') {
1240 ds_put_char(string, ' ');
1242 ofp_print_actions(string, fs.actions, fs.n_actions);
1247 ofp_print_ofpst_aggregate_reply(struct ds *string,
1248 const struct ofp_aggregate_stats_reply *asr)
1250 ds_put_format(string, " packet_count=%"PRIu64,
1251 ntohll(get_32aligned_be64(&asr->packet_count)));
1252 ds_put_format(string, " byte_count=%"PRIu64,
1253 ntohll(get_32aligned_be64(&asr->byte_count)));
1254 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1258 ofp_print_nxst_aggregate_reply(struct ds *string,
1259 const struct nx_aggregate_stats_reply *nasr)
1261 ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1262 ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1263 ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1266 static void print_port_stat(struct ds *string, const char *leader,
1267 const ovs_32aligned_be64 *statp, int more)
1269 uint64_t stat = ntohll(get_32aligned_be64(statp));
1271 ds_put_cstr(string, leader);
1272 if (stat != UINT64_MAX) {
1273 ds_put_format(string, "%"PRIu64, stat);
1275 ds_put_char(string, '?');
1278 ds_put_cstr(string, ", ");
1280 ds_put_cstr(string, "\n");
1285 ofp_print_ofpst_port_request(struct ds *string,
1286 const struct ofp_port_stats_request *psr)
1288 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1292 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1295 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1296 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1297 ds_put_format(string, " %zu ports\n", n);
1298 if (verbosity < 1) {
1303 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1305 ds_put_cstr(string, "rx ");
1306 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1307 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1308 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1309 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1310 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1311 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1312 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1314 ds_put_cstr(string, " tx ");
1315 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1316 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1317 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1318 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1319 print_port_stat(string, "coll=", &ps->collisions, 0);
1324 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1327 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1328 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1329 ds_put_format(string, " %zu tables\n", n);
1330 if (verbosity < 1) {
1335 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1336 ovs_strlcpy(name, ts->name, sizeof name);
1338 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1339 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1340 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1341 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1342 ds_put_cstr(string, " ");
1343 ds_put_format(string, "lookup=%"PRIu64", ",
1344 ntohll(get_32aligned_be64(&ts->lookup_count)));
1345 ds_put_format(string, "matched=%"PRIu64"\n",
1346 ntohll(get_32aligned_be64(&ts->matched_count)));
1351 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1353 if (queue_id == OFPQ_ALL) {
1354 ds_put_cstr(string, "ALL");
1356 ds_put_format(string, "%"PRIu32, queue_id);
1361 ofp_print_ofpst_queue_request(struct ds *string,
1362 const struct ofp_queue_stats_request *qsr)
1364 ds_put_cstr(string, "port=");
1365 ofputil_format_port(ntohs(qsr->port_no), string);
1367 ds_put_cstr(string, " queue=");
1368 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1372 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1375 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1376 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1377 ds_put_format(string, " %zu queues\n", n);
1378 if (verbosity < 1) {
1383 ds_put_cstr(string, " port ");
1384 ofputil_format_port(ntohs(qs->port_no), string);
1385 ds_put_cstr(string, " queue ");
1386 ofp_print_queue_name(string, ntohl(qs->queue_id));
1387 ds_put_cstr(string, ": ");
1389 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1390 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1391 print_port_stat(string, "errors=", &qs->tx_errors, 0);
1396 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1398 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1401 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1407 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1409 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1412 uint16_t flags = ntohs(srp->flags);
1414 ds_put_cstr(string, " flags=");
1415 if (flags & OFPSF_REPLY_MORE) {
1416 ds_put_cstr(string, "[more]");
1417 flags &= ~OFPSF_REPLY_MORE;
1420 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1427 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1429 size_t len = ntohs(oh->length);
1431 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1432 if (verbosity > 1) {
1433 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1438 ofp_print_nxt_role_message(struct ds *string,
1439 const struct nx_role_request *nrr)
1441 unsigned int role = ntohl(nrr->role);
1443 ds_put_cstr(string, " role=");
1444 if (role == NX_ROLE_OTHER) {
1445 ds_put_cstr(string, "other");
1446 } else if (role == NX_ROLE_MASTER) {
1447 ds_put_cstr(string, "master");
1448 } else if (role == NX_ROLE_SLAVE) {
1449 ds_put_cstr(string, "slave");
1451 ds_put_format(string, "%u", role);
1456 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1457 const struct nx_flow_mod_table_id *nfmti)
1459 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1463 ofp_print_nxt_set_flow_format(struct ds *string,
1464 const struct nx_set_flow_format *nsff)
1466 uint32_t format = ntohl(nsff->format);
1468 ds_put_cstr(string, " format=");
1469 if (ofputil_nx_flow_format_is_valid(format)) {
1470 ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1472 ds_put_format(string, "%"PRIu32, format);
1477 ofp_print_nxt_set_packet_in_format(struct ds *string,
1478 const struct nx_set_packet_in_format *nspf)
1480 uint32_t format = ntohl(nspf->format);
1482 ds_put_cstr(string, " format=");
1483 if (ofputil_packet_in_format_is_valid(format)) {
1484 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1486 ds_put_format(string, "%"PRIu32, format);
1491 ofp_port_reason_to_string(enum ofp_port_reason reason)
1506 sprintf(s, "%d", (int) reason);
1512 ofp_print_nxt_set_async_config(struct ds *string,
1513 const struct nx_async_config *nac)
1517 for (i = 0; i < 2; i++) {
1520 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1522 ds_put_cstr(string, " PACKET_IN:");
1523 for (j = 0; j < 32; j++) {
1524 if (nac->packet_in_mask[i] & htonl(1u << j)) {
1525 ds_put_format(string, " %s",
1526 ofputil_packet_in_reason_to_string(j));
1529 if (!nac->packet_in_mask[i]) {
1530 ds_put_cstr(string, " (off)");
1532 ds_put_char(string, '\n');
1534 ds_put_cstr(string, " PORT_STATUS:");
1535 for (j = 0; j < 32; j++) {
1536 if (nac->port_status_mask[i] & htonl(1u << j)) {
1537 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1540 if (!nac->port_status_mask[i]) {
1541 ds_put_cstr(string, " (off)");
1543 ds_put_char(string, '\n');
1545 ds_put_cstr(string, " FLOW_REMOVED:");
1546 for (j = 0; j < 32; j++) {
1547 if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1548 ds_put_format(string, " %s",
1549 ofp_flow_removed_reason_to_string(j));
1552 if (!nac->flow_removed_mask[i]) {
1553 ds_put_cstr(string, " (off)");
1555 ds_put_char(string, '\n');
1560 ofp_print_nxt_set_controller_id(struct ds *string,
1561 const struct nx_controller_id *nci)
1563 ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1567 ofp_to_string__(const struct ofp_header *oh,
1568 const struct ofputil_msg_type *type, struct ds *string,
1571 enum ofputil_msg_code code;
1572 const void *msg = oh;
1574 ds_put_cstr(string, ofputil_msg_type_name(type));
1575 switch (oh->version) {
1579 ds_put_cstr(string, " (OF1.1)");
1582 ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1585 ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1587 code = ofputil_msg_type_code(type);
1589 case OFPUTIL_MSG_INVALID:
1592 case OFPUTIL_OFPT_HELLO:
1593 ds_put_char(string, '\n');
1594 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1598 case OFPUTIL_OFPT_ERROR:
1599 ofp_print_error_msg(string, msg);
1602 case OFPUTIL_OFPT_ECHO_REQUEST:
1603 case OFPUTIL_OFPT_ECHO_REPLY:
1604 ofp_print_echo(string, oh, verbosity);
1607 case OFPUTIL_OFPT_FEATURES_REQUEST:
1610 case OFPUTIL_OFPT_FEATURES_REPLY:
1611 ofp_print_switch_features(string, msg);
1614 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1617 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1618 case OFPUTIL_OFPT_SET_CONFIG:
1619 ofp_print_switch_config(string, msg);
1622 case OFPUTIL_OFPT_PACKET_IN:
1623 case OFPUTIL_NXT_PACKET_IN:
1624 ofp_print_packet_in(string, msg, verbosity);
1627 case OFPUTIL_OFPT_FLOW_REMOVED:
1628 case OFPUTIL_NXT_FLOW_REMOVED:
1629 ofp_print_flow_removed(string, msg);
1632 case OFPUTIL_OFPT_PORT_STATUS:
1633 ofp_print_port_status(string, msg);
1636 case OFPUTIL_OFPT_PACKET_OUT:
1637 ofp_print_packet_out(string, msg, verbosity);
1640 case OFPUTIL_OFPT_FLOW_MOD:
1641 case OFPUTIL_NXT_FLOW_MOD:
1642 ofp_print_flow_mod(string, msg, code, verbosity);
1645 case OFPUTIL_OFPT_PORT_MOD:
1646 ofp_print_port_mod(string, msg);
1649 case OFPUTIL_OFPT_BARRIER_REQUEST:
1650 case OFPUTIL_OFPT_BARRIER_REPLY:
1653 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1654 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1658 case OFPUTIL_OFPST_DESC_REQUEST:
1659 ofp_print_stats_request(string, oh);
1662 case OFPUTIL_OFPST_FLOW_REQUEST:
1663 case OFPUTIL_NXST_FLOW_REQUEST:
1664 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1665 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1666 ofp_print_stats_request(string, oh);
1667 ofp_print_flow_stats_request(string, msg);
1670 case OFPUTIL_OFPST_TABLE_REQUEST:
1671 ofp_print_stats_request(string, oh);
1674 case OFPUTIL_OFPST_PORT_REQUEST:
1675 ofp_print_stats_request(string, oh);
1676 ofp_print_ofpst_port_request(string, msg);
1679 case OFPUTIL_OFPST_QUEUE_REQUEST:
1680 ofp_print_stats_request(string, oh);
1681 ofp_print_ofpst_queue_request(string, msg);
1684 case OFPUTIL_OFPST_DESC_REPLY:
1685 ofp_print_stats_reply(string, oh);
1686 ofp_print_ofpst_desc_reply(string, msg);
1689 case OFPUTIL_OFPST_FLOW_REPLY:
1690 case OFPUTIL_NXST_FLOW_REPLY:
1691 ofp_print_stats_reply(string, oh);
1692 ofp_print_flow_stats_reply(string, oh);
1695 case OFPUTIL_OFPST_QUEUE_REPLY:
1696 ofp_print_stats_reply(string, oh);
1697 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1700 case OFPUTIL_OFPST_PORT_REPLY:
1701 ofp_print_stats_reply(string, oh);
1702 ofp_print_ofpst_port_reply(string, oh, verbosity);
1705 case OFPUTIL_OFPST_TABLE_REPLY:
1706 ofp_print_stats_reply(string, oh);
1707 ofp_print_ofpst_table_reply(string, oh, verbosity);
1710 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1711 ofp_print_stats_reply(string, oh);
1712 ofp_print_ofpst_aggregate_reply(string, msg);
1715 case OFPUTIL_NXT_ROLE_REQUEST:
1716 case OFPUTIL_NXT_ROLE_REPLY:
1717 ofp_print_nxt_role_message(string, msg);
1720 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1721 ofp_print_nxt_flow_mod_table_id(string, msg);
1724 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1725 ofp_print_nxt_set_flow_format(string, msg);
1728 case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
1729 ofp_print_nxt_set_packet_in_format(string, msg);
1732 case OFPUTIL_NXT_FLOW_AGE:
1735 case OFPUTIL_NXT_SET_CONTROLLER_ID:
1736 ofp_print_nxt_set_controller_id(string, msg);
1739 case OFPUTIL_NXT_SET_ASYNC_CONFIG:
1740 ofp_print_nxt_set_async_config(string, msg);
1743 case OFPUTIL_NXST_AGGREGATE_REPLY:
1744 ofp_print_stats_reply(string, oh);
1745 ofp_print_nxst_aggregate_reply(string, msg);
1750 /* Composes and returns a string representing the OpenFlow packet of 'len'
1751 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1752 * verbosity and higher numbers increase verbosity. The caller is responsible
1753 * for freeing the string. */
1755 ofp_to_string(const void *oh_, size_t len, int verbosity)
1757 struct ds string = DS_EMPTY_INITIALIZER;
1758 const struct ofp_header *oh = oh_;
1761 ds_put_cstr(&string, "OpenFlow message is empty\n");
1762 } else if (len < sizeof(struct ofp_header)) {
1763 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1765 } else if (ntohs(oh->length) > len) {
1766 ds_put_format(&string,
1767 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1768 len, ntohs(oh->length));
1769 } else if (ntohs(oh->length) < len) {
1770 ds_put_format(&string,
1771 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1772 ntohs(oh->length), len);
1774 const struct ofputil_msg_type *type;
1777 error = ofputil_decode_msg_type(oh, &type);
1779 ofp_to_string__(oh, type, &string, verbosity);
1780 if (verbosity >= 5) {
1781 if (ds_last(&string) != '\n') {
1782 ds_put_char(&string, '\n');
1784 ds_put_hex_dump(&string, oh, len, 0, true);
1786 if (ds_last(&string) != '\n') {
1787 ds_put_char(&string, '\n');
1789 return ds_steal_cstr(&string);
1792 ofp_print_error(&string, error);
1794 ds_put_hex_dump(&string, oh, len, 0, true);
1795 return ds_steal_cstr(&string);
1799 print_and_free(FILE *stream, char *string)
1801 fputs(string, stream);
1805 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1806 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1807 * numbers increase verbosity. */
1809 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1811 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1814 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1815 * 'data' to 'stream'. */
1817 ofp_print_packet(FILE *stream, const void *data, size_t len)
1819 print_and_free(stream, ofp_packet_to_string(data, len));