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 if (pin.fmd.metadata_mask) {
117 ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
118 if (pin.fmd.metadata_mask != htonll(UINT64_MAX)) {
119 ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.metadata_mask));
123 for (i = 0; i < FLOW_N_REGS; i++) {
124 if (pin.fmd.reg_masks[i]) {
125 ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
126 if (pin.fmd.reg_masks[i] != UINT32_MAX) {
127 ds_put_format(string, "/0x%"PRIx32, pin.fmd.reg_masks[i]);
132 ds_put_format(string, " (via %s)",
133 ofputil_packet_in_reason_to_string(pin.reason));
135 ds_put_format(string, " data_len=%zu", pin.packet_len);
136 if (pin.buffer_id == UINT32_MAX) {
137 ds_put_format(string, " (unbuffered)");
138 if (pin.total_len != pin.packet_len) {
139 ds_put_format(string, " (***total_len != data_len***)");
142 ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
143 if (pin.total_len < pin.packet_len) {
144 ds_put_format(string, " (***total_len < data_len***)");
147 ds_put_char(string, '\n');
150 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
151 ds_put_cstr(string, packet);
157 print_note(struct ds *string, const struct nx_action_note *nan)
162 ds_put_cstr(string, "note:");
163 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
164 for (i = 0; i < len; i++) {
166 ds_put_char(string, '.');
168 ds_put_format(string, "%02"PRIx8, nan->note[i]);
173 ofp_print_action(struct ds *s, const union ofp_action *a,
174 enum ofputil_action_code code)
176 const struct ofp_action_enqueue *oae;
177 const struct ofp_action_dl_addr *oada;
178 const struct nx_action_set_tunnel64 *nast64;
179 const struct nx_action_set_tunnel *nast;
180 const struct nx_action_set_queue *nasq;
181 const struct nx_action_resubmit *nar;
182 const struct nx_action_reg_move *move;
183 const struct nx_action_reg_load *load;
184 const struct nx_action_multipath *nam;
185 const struct nx_action_autopath *naa;
186 const struct nx_action_output_reg *naor;
187 const struct nx_action_fin_timeout *naft;
188 const struct nx_action_controller *nac;
189 struct mf_subfield subfield;
193 case OFPUTIL_ACTION_INVALID:
196 case OFPUTIL_OFPAT10_OUTPUT:
197 port = ntohs(a->output.port);
198 if (port < OFPP_MAX) {
199 ds_put_format(s, "output:%"PRIu16, port);
201 ofputil_format_port(port, s);
202 if (port == OFPP_CONTROLLER) {
203 if (a->output.max_len != htons(0)) {
204 ds_put_format(s, ":%"PRIu16, ntohs(a->output.max_len));
206 ds_put_cstr(s, ":all");
212 case OFPUTIL_OFPAT10_ENQUEUE:
213 oae = (const struct ofp_action_enqueue *) a;
214 ds_put_format(s, "enqueue:");
215 ofputil_format_port(ntohs(oae->port), s);
216 ds_put_format(s, "q%"PRIu32, ntohl(oae->queue_id));
219 case OFPUTIL_OFPAT10_SET_VLAN_VID:
220 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
221 ntohs(a->vlan_vid.vlan_vid));
224 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
225 ds_put_format(s, "mod_vlan_pcp:%"PRIu8, a->vlan_pcp.vlan_pcp);
228 case OFPUTIL_OFPAT10_STRIP_VLAN:
229 ds_put_cstr(s, "strip_vlan");
232 case OFPUTIL_OFPAT10_SET_DL_SRC:
233 oada = (const struct ofp_action_dl_addr *) a;
234 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
235 ETH_ADDR_ARGS(oada->dl_addr));
238 case OFPUTIL_OFPAT10_SET_DL_DST:
239 oada = (const struct ofp_action_dl_addr *) a;
240 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
241 ETH_ADDR_ARGS(oada->dl_addr));
244 case OFPUTIL_OFPAT10_SET_NW_SRC:
245 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
248 case OFPUTIL_OFPAT10_SET_NW_DST:
249 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
252 case OFPUTIL_OFPAT10_SET_NW_TOS:
253 ds_put_format(s, "mod_nw_tos:%d", a->nw_tos.nw_tos);
256 case OFPUTIL_OFPAT10_SET_TP_SRC:
257 ds_put_format(s, "mod_tp_src:%d", ntohs(a->tp_port.tp_port));
260 case OFPUTIL_OFPAT10_SET_TP_DST:
261 ds_put_format(s, "mod_tp_dst:%d", ntohs(a->tp_port.tp_port));
264 case OFPUTIL_NXAST_RESUBMIT:
265 nar = (struct nx_action_resubmit *)a;
266 ds_put_format(s, "resubmit:");
267 ofputil_format_port(ntohs(nar->in_port), s);
270 case OFPUTIL_NXAST_RESUBMIT_TABLE:
271 nar = (struct nx_action_resubmit *)a;
272 ds_put_format(s, "resubmit(");
273 if (nar->in_port != htons(OFPP_IN_PORT)) {
274 ofputil_format_port(ntohs(nar->in_port), s);
277 if (nar->table != 255) {
278 ds_put_format(s, "%"PRIu8, nar->table);
283 case OFPUTIL_NXAST_SET_TUNNEL:
284 nast = (struct nx_action_set_tunnel *)a;
285 ds_put_format(s, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
288 case OFPUTIL_NXAST_SET_QUEUE:
289 nasq = (struct nx_action_set_queue *)a;
290 ds_put_format(s, "set_queue:%u", ntohl(nasq->queue_id));
293 case OFPUTIL_NXAST_POP_QUEUE:
294 ds_put_cstr(s, "pop_queue");
297 case OFPUTIL_NXAST_NOTE:
298 print_note(s, (const struct nx_action_note *) a);
301 case OFPUTIL_NXAST_REG_MOVE:
302 move = (const struct nx_action_reg_move *) a;
303 nxm_format_reg_move(move, s);
306 case OFPUTIL_NXAST_REG_LOAD:
307 load = (const struct nx_action_reg_load *) a;
308 nxm_format_reg_load(load, s);
311 case OFPUTIL_NXAST_SET_TUNNEL64:
312 nast64 = (const struct nx_action_set_tunnel64 *) a;
313 ds_put_format(s, "set_tunnel64:%#"PRIx64,
314 ntohll(nast64->tun_id));
317 case OFPUTIL_NXAST_MULTIPATH:
318 nam = (const struct nx_action_multipath *) a;
319 multipath_format(nam, s);
322 case OFPUTIL_NXAST_AUTOPATH:
323 naa = (const struct nx_action_autopath *)a;
324 ds_put_format(s, "autopath(%u,", ntohl(naa->id));
325 nxm_decode(&subfield, naa->dst, naa->ofs_nbits);
326 mf_format_subfield(&subfield, s);
330 case OFPUTIL_NXAST_BUNDLE:
331 case OFPUTIL_NXAST_BUNDLE_LOAD:
332 bundle_format((const struct nx_action_bundle *) a, s);
335 case OFPUTIL_NXAST_OUTPUT_REG:
336 naor = (const struct nx_action_output_reg *) a;
337 ds_put_cstr(s, "output:");
338 nxm_decode(&subfield, naor->src, naor->ofs_nbits);
339 mf_format_subfield(&subfield, s);
342 case OFPUTIL_NXAST_LEARN:
343 learn_format((const struct nx_action_learn *) a, s);
346 case OFPUTIL_NXAST_DEC_TTL:
347 ds_put_cstr(s, "dec_ttl");
350 case OFPUTIL_NXAST_EXIT:
351 ds_put_cstr(s, "exit");
354 case OFPUTIL_NXAST_FIN_TIMEOUT:
355 naft = (const struct nx_action_fin_timeout *) a;
356 ds_put_cstr(s, "fin_timeout(");
357 if (naft->fin_idle_timeout) {
358 ds_put_format(s, "idle_timeout=%"PRIu16",",
359 ntohs(naft->fin_idle_timeout));
361 if (naft->fin_hard_timeout) {
362 ds_put_format(s, "hard_timeout=%"PRIu16",",
363 ntohs(naft->fin_hard_timeout));
369 case OFPUTIL_NXAST_CONTROLLER:
370 nac = (const struct nx_action_controller *) a;
371 ds_put_cstr(s, "controller(");
372 if (nac->reason != OFPR_ACTION) {
373 ds_put_format(s, "reason=%s,",
374 ofputil_packet_in_reason_to_string(nac->reason));
376 if (nac->max_len != htons(UINT16_MAX)) {
377 ds_put_format(s, "max_len=%"PRIu16",", ntohs(nac->max_len));
379 if (nac->controller_id != htons(0)) {
380 ds_put_format(s, "id=%"PRIu16",", ntohs(nac->controller_id));
392 ofp_print_actions(struct ds *string, const union ofp_action *actions,
395 const union ofp_action *a;
398 ds_put_cstr(string, "actions=");
400 ds_put_cstr(string, "drop");
403 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
404 int code = ofputil_decode_action(a);
407 ds_put_cstr(string, ",");
409 ofp_print_action(string, a, code);
411 ofp_print_error(string, -code);
415 ds_put_format(string, " ***%zu leftover bytes following actions",
421 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
424 struct ofputil_packet_out po;
427 error = ofputil_decode_packet_out(&po, opo);
429 ofp_print_error(string, error);
433 ds_put_cstr(string, " in_port=");
434 ofputil_format_port(po.in_port, string);
436 ds_put_char(string, ' ');
437 ofp_print_actions(string, po.actions, po.n_actions);
439 if (po.buffer_id == UINT32_MAX) {
440 ds_put_format(string, " data_len=%zu", po.packet_len);
441 if (verbosity > 0 && po.packet_len > 0) {
442 char *packet = ofp_packet_to_string(po.packet, po.packet_len);
443 ds_put_char(string, '\n');
444 ds_put_cstr(string, packet);
448 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
450 ds_put_char(string, '\n');
453 /* qsort comparison function. */
455 compare_ports(const void *a_, const void *b_)
457 const struct ofputil_phy_port *a = a_;
458 const struct ofputil_phy_port *b = b_;
459 uint16_t ap = a->port_no;
460 uint16_t bp = b->port_no;
462 return ap < bp ? -1 : ap > bp;
466 ofp_print_bit_names(struct ds *string, uint32_t bits,
467 const char *(*bit_to_name)(uint32_t bit))
473 ds_put_cstr(string, "0");
477 for (i = 0; i < 32; i++) {
478 uint32_t bit = UINT32_C(1) << i;
481 const char *name = bit_to_name(bit);
484 ds_put_char(string, ' ');
486 ds_put_cstr(string, name);
494 ds_put_char(string, ' ');
496 ds_put_format(string, "0x%"PRIx32, bits);
501 netdev_feature_to_name(uint32_t bit)
503 enum netdev_features f = bit;
506 case NETDEV_F_10MB_HD: return "10MB-HD";
507 case NETDEV_F_10MB_FD: return "10MB-FD";
508 case NETDEV_F_100MB_HD: return "100MB-HD";
509 case NETDEV_F_100MB_FD: return "100MB-FD";
510 case NETDEV_F_1GB_HD: return "1GB-HD";
511 case NETDEV_F_1GB_FD: return "1GB-FD";
512 case NETDEV_F_10GB_FD: return "10GB-FD";
513 case NETDEV_F_40GB_FD: return "40GB-FD";
514 case NETDEV_F_100GB_FD: return "100GB-FD";
515 case NETDEV_F_1TB_FD: return "1TB-FD";
516 case NETDEV_F_OTHER: return "OTHER";
517 case NETDEV_F_COPPER: return "COPPER";
518 case NETDEV_F_FIBER: return "FIBER";
519 case NETDEV_F_AUTONEG: return "AUTO_NEG";
520 case NETDEV_F_PAUSE: return "AUTO_PAUSE";
521 case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
528 ofp_print_port_features(struct ds *string, enum netdev_features features)
530 ofp_print_bit_names(string, features, netdev_feature_to_name);
531 ds_put_char(string, '\n');
535 ofputil_port_config_to_name(uint32_t bit)
537 enum ofputil_port_config pc = bit;
540 case OFPUTIL_PC_PORT_DOWN: return "PORT_DOWN";
541 case OFPUTIL_PC_NO_STP: return "NO_STP";
542 case OFPUTIL_PC_NO_RECV: return "NO_RECV";
543 case OFPUTIL_PC_NO_RECV_STP: return "NO_RECV_STP";
544 case OFPUTIL_PC_NO_FLOOD: return "NO_FLOOD";
545 case OFPUTIL_PC_NO_FWD: return "NO_FWD";
546 case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
553 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
555 ofp_print_bit_names(string, config, ofputil_port_config_to_name);
556 ds_put_char(string, '\n');
560 ofputil_port_state_to_name(uint32_t bit)
562 enum ofputil_port_state ps = bit;
565 case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
566 case OFPUTIL_PS_BLOCKED: return "BLOCKED";
567 case OFPUTIL_PS_LIVE: return "LIVE";
569 case OFPUTIL_PS_STP_LISTEN:
570 case OFPUTIL_PS_STP_LEARN:
571 case OFPUTIL_PS_STP_FORWARD:
572 case OFPUTIL_PS_STP_BLOCK:
573 /* Handled elsewhere. */
581 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
583 enum ofputil_port_state stp_state;
585 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
586 * pattern. We have to special case it.
588 * OVS doesn't support STP, so this field will always be 0 if we are
589 * talking to OVS, so we'd always print STP_LISTEN in that case.
590 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
591 * avoid confusing users. */
592 stp_state = state & OFPUTIL_PS_STP_MASK;
595 (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
596 : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
598 state &= ~OFPUTIL_PS_STP_MASK;
600 ofp_print_bit_names(string, state, ofputil_port_state_to_name);
603 ofp_print_bit_names(string, state, ofputil_port_state_to_name);
605 ds_put_char(string, '\n');
609 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
611 char name[sizeof port->name];
614 memcpy(name, port->name, sizeof name);
615 for (j = 0; j < sizeof name - 1; j++) {
616 if (!isprint((unsigned char) name[j])) {
622 ds_put_char(string, ' ');
623 ofputil_format_port(port->port_no, string);
624 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
625 name, ETH_ADDR_ARGS(port->hw_addr));
627 ds_put_cstr(string, " config: ");
628 ofp_print_port_config(string, port->config);
630 ds_put_cstr(string, " state: ");
631 ofp_print_port_state(string, port->state);
634 ds_put_format(string, " current: ");
635 ofp_print_port_features(string, port->curr);
637 if (port->advertised) {
638 ds_put_format(string, " advertised: ");
639 ofp_print_port_features(string, port->advertised);
641 if (port->supported) {
642 ds_put_format(string, " supported: ");
643 ofp_print_port_features(string, port->supported);
646 ds_put_format(string, " peer: ");
647 ofp_print_port_features(string, port->peer);
649 ds_put_format(string, " speed: %"PRIu32" Mbps now, "
650 "%"PRIu32" Mbps max\n",
651 port->curr_speed / UINT32_C(1000),
652 port->max_speed / UINT32_C(1000));
655 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
656 * 'ofp_version', writes a detailed description of each port into
659 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
663 struct ofputil_phy_port *ports;
667 n_ports = ofputil_count_phy_ports(ofp_version, b);
669 ports = xmalloc(n_ports * sizeof *ports);
670 for (i = 0; i < n_ports; i++) {
671 error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
673 ofp_print_error(string, error);
677 qsort(ports, n_ports, sizeof *ports, compare_ports);
678 for (i = 0; i < n_ports; i++) {
679 ofp_print_phy_port(string, &ports[i]);
687 ofputil_capabilities_to_name(uint32_t bit)
689 enum ofputil_capabilities capabilities = bit;
691 switch (capabilities) {
692 case OFPUTIL_C_FLOW_STATS: return "FLOW_STATS";
693 case OFPUTIL_C_TABLE_STATS: return "TABLE_STATS";
694 case OFPUTIL_C_PORT_STATS: return "PORT_STATS";
695 case OFPUTIL_C_IP_REASM: return "IP_REASM";
696 case OFPUTIL_C_QUEUE_STATS: return "QUEUE_STATS";
697 case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
698 case OFPUTIL_C_STP: return "STP";
699 case OFPUTIL_C_GROUP_STATS: return "GROUP_STATS";
706 ofputil_action_bitmap_to_name(uint32_t bit)
708 enum ofputil_action_bitmap action = bit;
711 case OFPUTIL_A_OUTPUT: return "OUTPUT";
712 case OFPUTIL_A_SET_VLAN_VID: return "SET_VLAN_VID";
713 case OFPUTIL_A_SET_VLAN_PCP: return "SET_VLAN_PCP";
714 case OFPUTIL_A_STRIP_VLAN: return "STRIP_VLAN";
715 case OFPUTIL_A_SET_DL_SRC: return "SET_DL_SRC";
716 case OFPUTIL_A_SET_DL_DST: return "SET_DL_DST";
717 case OFPUTIL_A_SET_NW_SRC: return "SET_NW_SRC";
718 case OFPUTIL_A_SET_NW_DST: return "SET_NW_DST";
719 case OFPUTIL_A_SET_NW_ECN: return "SET_NW_ECN";
720 case OFPUTIL_A_SET_NW_TOS: return "SET_NW_TOS";
721 case OFPUTIL_A_SET_TP_SRC: return "SET_TP_SRC";
722 case OFPUTIL_A_SET_TP_DST: return "SET_TP_DST";
723 case OFPUTIL_A_ENQUEUE: return "ENQUEUE";
724 case OFPUTIL_A_COPY_TTL_OUT: return "COPY_TTL_OUT";
725 case OFPUTIL_A_COPY_TTL_IN: return "COPY_TTL_IN";
726 case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
727 case OFPUTIL_A_SET_MPLS_TC: return "SET_MPLS_TC";
728 case OFPUTIL_A_SET_MPLS_TTL: return "SET_MPLS_TTL";
729 case OFPUTIL_A_DEC_MPLS_TTL: return "DEC_MPLS_TTL";
730 case OFPUTIL_A_PUSH_VLAN: return "PUSH_VLAN";
731 case OFPUTIL_A_POP_VLAN: return "POP_VLAN";
732 case OFPUTIL_A_PUSH_MPLS: return "PUSH_MPLS";
733 case OFPUTIL_A_POP_MPLS: return "POP_MPLS";
734 case OFPUTIL_A_SET_QUEUE: return "SET_QUEUE";
735 case OFPUTIL_A_GROUP: return "GROUP";
736 case OFPUTIL_A_SET_NW_TTL: return "SET_NW_TTL";
737 case OFPUTIL_A_DEC_NW_TTL: return "DEC_NW_TTL";
744 ofp_print_switch_features(struct ds *string,
745 const struct ofp_switch_features *osf)
747 struct ofputil_switch_features features;
751 error = ofputil_decode_switch_features(osf, &features, &b);
753 ofp_print_error(string, error);
757 ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
758 ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32"\n",
759 features.n_tables, features.n_buffers);
761 ds_put_cstr(string, "capabilities: ");
762 ofp_print_bit_names(string, features.capabilities,
763 ofputil_capabilities_to_name);
764 ds_put_char(string, '\n');
766 ds_put_cstr(string, "actions: ");
767 ofp_print_bit_names(string, features.actions,
768 ofputil_action_bitmap_to_name);
769 ds_put_char(string, '\n');
771 ofp_print_phy_ports(string, osf->header.version, &b);
775 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
777 enum ofp_config_flags flags;
779 flags = ntohs(osc->flags);
781 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
782 flags &= ~OFPC_FRAG_MASK;
784 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
785 ds_put_format(string, " invalid_ttl_to_controller");
786 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
790 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
793 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
796 static void print_wild(struct ds *string, const char *leader, int is_wild,
797 int verbosity, const char *format, ...)
798 __attribute__((format(printf, 5, 6)));
800 static void print_wild(struct ds *string, const char *leader, int is_wild,
801 int verbosity, const char *format, ...)
803 if (is_wild && verbosity < 2) {
806 ds_put_cstr(string, leader);
810 va_start(args, format);
811 ds_put_format_valist(string, format, args);
814 ds_put_char(string, '*');
816 ds_put_char(string, ',');
820 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
821 uint32_t wild_bits, int verbosity)
823 if (wild_bits >= 32 && verbosity < 2) {
826 ds_put_cstr(string, leader);
827 if (wild_bits < 32) {
828 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
830 ds_put_format(string, "/%d", 32 - wild_bits);
833 ds_put_char(string, '*');
835 ds_put_char(string, ',');
839 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
841 char *s = ofp10_match_to_string(om, verbosity);
847 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
849 struct ds f = DS_EMPTY_INITIALIZER;
850 uint32_t w = ntohl(om->wildcards);
851 bool skip_type = false;
852 bool skip_proto = false;
854 if (!(w & OFPFW10_DL_TYPE)) {
856 if (om->dl_type == htons(ETH_TYPE_IP)) {
857 if (!(w & OFPFW10_NW_PROTO)) {
859 if (om->nw_proto == IPPROTO_ICMP) {
860 ds_put_cstr(&f, "icmp,");
861 } else if (om->nw_proto == IPPROTO_TCP) {
862 ds_put_cstr(&f, "tcp,");
863 } else if (om->nw_proto == IPPROTO_UDP) {
864 ds_put_cstr(&f, "udp,");
866 ds_put_cstr(&f, "ip,");
870 ds_put_cstr(&f, "ip,");
872 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
873 ds_put_cstr(&f, "arp,");
878 print_wild(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
879 "%d", ntohs(om->in_port));
880 print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
881 "%d", ntohs(om->dl_vlan));
882 print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
883 "%d", om->dl_vlan_pcp);
884 print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
885 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
886 print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
887 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
889 print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
890 "0x%04x", ntohs(om->dl_type));
892 print_ip_netmask(&f, "nw_src=", om->nw_src,
893 (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
895 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
896 (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
899 if (om->dl_type == htons(ETH_TYPE_ARP)) {
900 print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
903 print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
907 print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
909 if (om->nw_proto == IPPROTO_ICMP) {
910 print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
911 "%d", ntohs(om->tp_src));
912 print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
913 "%d", ntohs(om->tp_dst));
915 print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
916 "%d", ntohs(om->tp_src));
917 print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
918 "%d", ntohs(om->tp_dst));
920 if (ds_last(&f) == ',') {
927 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
928 enum ofputil_msg_code code, int verbosity)
930 struct ofputil_flow_mod fm;
934 error = ofputil_decode_flow_mod(&fm, oh, OFPUTIL_P_OF10_TID);
936 ofp_print_error(s, error);
941 switch (fm.command) {
943 ds_put_cstr(s, "ADD");
946 ds_put_cstr(s, "MOD");
948 case OFPFC_MODIFY_STRICT:
949 ds_put_cstr(s, "MOD_STRICT");
952 ds_put_cstr(s, "DEL");
954 case OFPFC_DELETE_STRICT:
955 ds_put_cstr(s, "DEL_STRICT");
958 ds_put_format(s, "cmd:%d", fm.command);
960 if (fm.table_id != 0) {
961 ds_put_format(s, " table:%d", fm.table_id);
965 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
966 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
967 ofp10_match_print(s, &ofm->match, verbosity);
969 /* ofp_print_match() doesn't print priority. */
970 need_priority = true;
971 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
972 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
973 const void *nxm = nfm + 1;
976 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
977 ds_put_cstr(s, nxm_s);
980 /* nx_match_to_string() doesn't print priority. */
981 need_priority = true;
983 cls_rule_format(&fm.cr, s);
985 /* cls_rule_format() does print priority. */
986 need_priority = false;
989 if (ds_last(s) != ' ') {
992 if (fm.new_cookie != htonll(0)) {
993 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
995 if (fm.cookie_mask != htonll(0)) {
996 ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
997 ntohll(fm.cookie), ntohll(fm.cookie_mask));
999 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
1000 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
1002 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
1003 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
1005 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
1006 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
1008 if (fm.buffer_id != UINT32_MAX) {
1009 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
1011 if (fm.flags != 0) {
1012 uint16_t flags = fm.flags;
1014 if (flags & OFPFF_SEND_FLOW_REM) {
1015 ds_put_cstr(s, "send_flow_rem ");
1017 if (flags & OFPFF_CHECK_OVERLAP) {
1018 ds_put_cstr(s, "check_overlap ");
1020 if (flags & OFPFF_EMERG) {
1021 ds_put_cstr(s, "emerg ");
1024 flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP | OFPFF_EMERG);
1026 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
1030 ofp_print_actions(s, fm.actions, fm.n_actions);
1034 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
1036 ds_put_format(string, "%u", sec);
1038 ds_put_format(string, ".%09u", nsec);
1039 while (string->string[string->length - 1] == '0') {
1043 ds_put_char(string, 's');
1047 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
1052 case OFPRR_IDLE_TIMEOUT:
1054 case OFPRR_HARD_TIMEOUT:
1058 case OFPRR_GROUP_DELETE:
1059 return "group_delete";
1061 sprintf(s, "%d", (int) reason);
1067 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
1069 struct ofputil_flow_removed fr;
1072 error = ofputil_decode_flow_removed(&fr, oh);
1074 ofp_print_error(string, error);
1078 ds_put_char(string, ' ');
1079 cls_rule_format(&fr.rule, string);
1081 ds_put_format(string, " reason=%s",
1082 ofp_flow_removed_reason_to_string(fr.reason));
1084 if (fr.cookie != htonll(0)) {
1085 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
1087 ds_put_cstr(string, " duration");
1088 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
1089 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
1090 fr.idle_timeout, fr.packet_count, fr.byte_count);
1094 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
1096 struct ofputil_port_mod pm;
1099 error = ofputil_decode_port_mod(oh, &pm);
1101 ofp_print_error(string, error);
1105 ds_put_format(string, "port: %"PRIu16": addr:"ETH_ADDR_FMT"\n",
1106 pm.port_no, ETH_ADDR_ARGS(pm.hw_addr));
1108 ds_put_format(string, " config: ");
1109 ofp_print_port_config(string, pm.config);
1111 ds_put_format(string, " mask: ");
1112 ofp_print_port_config(string, pm.mask);
1114 ds_put_format(string, " advertise: ");
1116 ofp_print_port_features(string, pm.advertise);
1118 ds_put_format(string, "UNCHANGED\n");
1123 ofp_print_error(struct ds *string, enum ofperr error)
1125 if (string->length) {
1126 ds_put_char(string, ' ');
1128 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1132 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1134 size_t len = ntohs(oem->header.length);
1135 size_t payload_ofs, payload_len;
1136 const void *payload;
1140 error = ofperr_decode_msg(&oem->header, &payload_ofs);
1142 ds_put_cstr(string, "***decode error***");
1143 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1147 ds_put_format(string, " %s\n", ofperr_get_name(error));
1149 payload = (const uint8_t *) oem + payload_ofs;
1150 payload_len = len - payload_ofs;
1151 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1152 ds_put_printable(string, payload, payload_len);
1154 s = ofp_to_string(payload, payload_len, 1);
1155 ds_put_cstr(string, s);
1161 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1163 struct ofputil_port_status ps;
1166 error = ofputil_decode_port_status(ops, &ps);
1168 ofp_print_error(string, error);
1172 if (ps.reason == OFPPR_ADD) {
1173 ds_put_format(string, " ADD:");
1174 } else if (ps.reason == OFPPR_DELETE) {
1175 ds_put_format(string, " DEL:");
1176 } else if (ps.reason == OFPPR_MODIFY) {
1177 ds_put_format(string, " MOD:");
1180 ofp_print_phy_port(string, &ps.desc);
1184 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
1186 ds_put_char(string, '\n');
1187 ds_put_format(string, "Manufacturer: %.*s\n",
1188 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1189 ds_put_format(string, "Hardware: %.*s\n",
1190 (int) sizeof ods->hw_desc, ods->hw_desc);
1191 ds_put_format(string, "Software: %.*s\n",
1192 (int) sizeof ods->sw_desc, ods->sw_desc);
1193 ds_put_format(string, "Serial Num: %.*s\n",
1194 (int) sizeof ods->serial_num, ods->serial_num);
1195 ds_put_format(string, "DP Description: %.*s\n",
1196 (int) sizeof ods->dp_desc, ods->dp_desc);
1200 ofp_print_flow_stats_request(struct ds *string,
1201 const struct ofp_stats_msg *osm)
1203 struct ofputil_flow_stats_request fsr;
1206 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
1208 ofp_print_error(string, error);
1212 if (fsr.table_id != 0xff) {
1213 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1216 if (fsr.out_port != OFPP_NONE) {
1217 ds_put_cstr(string, " out_port=");
1218 ofputil_format_port(fsr.out_port, string);
1221 /* A flow stats request doesn't include a priority, but cls_rule_format()
1222 * will print one unless it is OFP_DEFAULT_PRIORITY. */
1223 fsr.match.priority = OFP_DEFAULT_PRIORITY;
1225 ds_put_char(string, ' ');
1226 cls_rule_format(&fsr.match, string);
1230 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1234 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1236 struct ofputil_flow_stats fs;
1239 retval = ofputil_decode_flow_stats_reply(&fs, &b, true);
1241 if (retval != EOF) {
1242 ds_put_cstr(string, " ***parse error***");
1247 ds_put_char(string, '\n');
1249 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1251 ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
1252 ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
1253 ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
1254 ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
1255 if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
1256 ds_put_format(string, "idle_timeout=%"PRIu16", ", fs.idle_timeout);
1258 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1259 ds_put_format(string, "hard_timeout=%"PRIu16", ", fs.hard_timeout);
1261 if (fs.idle_age >= 0) {
1262 ds_put_format(string, "idle_age=%d, ", fs.idle_age);
1264 if (fs.hard_age >= 0 && fs.hard_age != fs.duration_sec) {
1265 ds_put_format(string, "hard_age=%d, ", fs.hard_age);
1268 cls_rule_format(&fs.rule, string);
1269 if (string->string[string->length - 1] != ' ') {
1270 ds_put_char(string, ' ');
1272 ofp_print_actions(string, fs.actions, fs.n_actions);
1277 ofp_print_ofpst_aggregate_reply(struct ds *string,
1278 const struct ofp_aggregate_stats_reply *asr)
1280 ds_put_format(string, " packet_count=%"PRIu64,
1281 ntohll(get_32aligned_be64(&asr->packet_count)));
1282 ds_put_format(string, " byte_count=%"PRIu64,
1283 ntohll(get_32aligned_be64(&asr->byte_count)));
1284 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1288 ofp_print_nxst_aggregate_reply(struct ds *string,
1289 const struct nx_aggregate_stats_reply *nasr)
1291 ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1292 ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1293 ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1296 static void print_port_stat(struct ds *string, const char *leader,
1297 const ovs_32aligned_be64 *statp, int more)
1299 uint64_t stat = ntohll(get_32aligned_be64(statp));
1301 ds_put_cstr(string, leader);
1302 if (stat != UINT64_MAX) {
1303 ds_put_format(string, "%"PRIu64, stat);
1305 ds_put_char(string, '?');
1308 ds_put_cstr(string, ", ");
1310 ds_put_cstr(string, "\n");
1315 ofp_print_ofpst_port_request(struct ds *string,
1316 const struct ofp_port_stats_request *psr)
1318 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1322 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1325 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1326 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1327 ds_put_format(string, " %zu ports\n", n);
1328 if (verbosity < 1) {
1333 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1335 ds_put_cstr(string, "rx ");
1336 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1337 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1338 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1339 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1340 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1341 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1342 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1344 ds_put_cstr(string, " tx ");
1345 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1346 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1347 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1348 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1349 print_port_stat(string, "coll=", &ps->collisions, 0);
1354 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1357 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1358 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1359 ds_put_format(string, " %zu tables\n", n);
1360 if (verbosity < 1) {
1365 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1366 ovs_strlcpy(name, ts->name, sizeof name);
1368 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1369 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1370 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1371 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1372 ds_put_cstr(string, " ");
1373 ds_put_format(string, "lookup=%"PRIu64", ",
1374 ntohll(get_32aligned_be64(&ts->lookup_count)));
1375 ds_put_format(string, "matched=%"PRIu64"\n",
1376 ntohll(get_32aligned_be64(&ts->matched_count)));
1381 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1383 if (queue_id == OFPQ_ALL) {
1384 ds_put_cstr(string, "ALL");
1386 ds_put_format(string, "%"PRIu32, queue_id);
1391 ofp_print_ofpst_queue_request(struct ds *string,
1392 const struct ofp_queue_stats_request *qsr)
1394 ds_put_cstr(string, "port=");
1395 ofputil_format_port(ntohs(qsr->port_no), string);
1397 ds_put_cstr(string, " queue=");
1398 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1402 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1405 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1406 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1407 ds_put_format(string, " %zu queues\n", n);
1408 if (verbosity < 1) {
1413 ds_put_cstr(string, " port ");
1414 ofputil_format_port(ntohs(qs->port_no), string);
1415 ds_put_cstr(string, " queue ");
1416 ofp_print_queue_name(string, ntohl(qs->queue_id));
1417 ds_put_cstr(string, ": ");
1419 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1420 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1421 print_port_stat(string, "errors=", &qs->tx_errors, 0);
1426 ofp_print_ofpst_port_desc_reply(struct ds *string,
1427 const struct ofp_header *oh)
1431 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1432 ofpbuf_pull(&b, sizeof(struct ofp_stats_msg));
1433 ds_put_char(string, '\n');
1434 ofp_print_phy_ports(string, oh->version, &b);
1438 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1440 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1443 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1449 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1451 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1454 uint16_t flags = ntohs(srp->flags);
1456 ds_put_cstr(string, " flags=");
1457 if (flags & OFPSF_REPLY_MORE) {
1458 ds_put_cstr(string, "[more]");
1459 flags &= ~OFPSF_REPLY_MORE;
1462 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1469 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1471 size_t len = ntohs(oh->length);
1473 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1474 if (verbosity > 1) {
1475 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1480 ofp_print_nxt_role_message(struct ds *string,
1481 const struct nx_role_request *nrr)
1483 unsigned int role = ntohl(nrr->role);
1485 ds_put_cstr(string, " role=");
1486 if (role == NX_ROLE_OTHER) {
1487 ds_put_cstr(string, "other");
1488 } else if (role == NX_ROLE_MASTER) {
1489 ds_put_cstr(string, "master");
1490 } else if (role == NX_ROLE_SLAVE) {
1491 ds_put_cstr(string, "slave");
1493 ds_put_format(string, "%u", role);
1498 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1499 const struct nx_flow_mod_table_id *nfmti)
1501 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1505 ofp_print_nxt_set_flow_format(struct ds *string,
1506 const struct nx_set_flow_format *nsff)
1508 uint32_t format = ntohl(nsff->format);
1510 ds_put_cstr(string, " format=");
1511 if (ofputil_nx_flow_format_is_valid(format)) {
1512 ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1514 ds_put_format(string, "%"PRIu32, format);
1519 ofp_print_nxt_set_packet_in_format(struct ds *string,
1520 const struct nx_set_packet_in_format *nspf)
1522 uint32_t format = ntohl(nspf->format);
1524 ds_put_cstr(string, " format=");
1525 if (ofputil_packet_in_format_is_valid(format)) {
1526 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1528 ds_put_format(string, "%"PRIu32, format);
1533 ofp_port_reason_to_string(enum ofp_port_reason reason)
1548 sprintf(s, "%d", (int) reason);
1554 ofp_print_nxt_set_async_config(struct ds *string,
1555 const struct nx_async_config *nac)
1559 for (i = 0; i < 2; i++) {
1562 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1564 ds_put_cstr(string, " PACKET_IN:");
1565 for (j = 0; j < 32; j++) {
1566 if (nac->packet_in_mask[i] & htonl(1u << j)) {
1567 ds_put_format(string, " %s",
1568 ofputil_packet_in_reason_to_string(j));
1571 if (!nac->packet_in_mask[i]) {
1572 ds_put_cstr(string, " (off)");
1574 ds_put_char(string, '\n');
1576 ds_put_cstr(string, " PORT_STATUS:");
1577 for (j = 0; j < 32; j++) {
1578 if (nac->port_status_mask[i] & htonl(1u << j)) {
1579 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1582 if (!nac->port_status_mask[i]) {
1583 ds_put_cstr(string, " (off)");
1585 ds_put_char(string, '\n');
1587 ds_put_cstr(string, " FLOW_REMOVED:");
1588 for (j = 0; j < 32; j++) {
1589 if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1590 ds_put_format(string, " %s",
1591 ofp_flow_removed_reason_to_string(j));
1594 if (!nac->flow_removed_mask[i]) {
1595 ds_put_cstr(string, " (off)");
1597 ds_put_char(string, '\n');
1602 ofp_print_nxt_set_controller_id(struct ds *string,
1603 const struct nx_controller_id *nci)
1605 ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1609 ofp_to_string__(const struct ofp_header *oh,
1610 const struct ofputil_msg_type *type, struct ds *string,
1613 enum ofputil_msg_code code;
1614 const void *msg = oh;
1616 ds_put_cstr(string, ofputil_msg_type_name(type));
1617 switch (oh->version) {
1621 ds_put_cstr(string, " (OF1.1)");
1624 ds_put_cstr(string, " (OF1.2)");
1627 ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1630 ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1632 code = ofputil_msg_type_code(type);
1634 case OFPUTIL_MSG_INVALID:
1637 case OFPUTIL_OFPT_HELLO:
1638 ds_put_char(string, '\n');
1639 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1643 case OFPUTIL_OFPT_ERROR:
1644 ofp_print_error_msg(string, msg);
1647 case OFPUTIL_OFPT_ECHO_REQUEST:
1648 case OFPUTIL_OFPT_ECHO_REPLY:
1649 ofp_print_echo(string, oh, verbosity);
1652 case OFPUTIL_OFPT_FEATURES_REQUEST:
1655 case OFPUTIL_OFPT_FEATURES_REPLY:
1656 ofp_print_switch_features(string, msg);
1659 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1662 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1663 case OFPUTIL_OFPT_SET_CONFIG:
1664 ofp_print_switch_config(string, msg);
1667 case OFPUTIL_OFPT_PACKET_IN:
1668 case OFPUTIL_NXT_PACKET_IN:
1669 ofp_print_packet_in(string, msg, verbosity);
1672 case OFPUTIL_OFPT_FLOW_REMOVED:
1673 case OFPUTIL_NXT_FLOW_REMOVED:
1674 ofp_print_flow_removed(string, msg);
1677 case OFPUTIL_OFPT_PORT_STATUS:
1678 ofp_print_port_status(string, msg);
1681 case OFPUTIL_OFPT_PACKET_OUT:
1682 ofp_print_packet_out(string, msg, verbosity);
1685 case OFPUTIL_OFPT_FLOW_MOD:
1686 case OFPUTIL_NXT_FLOW_MOD:
1687 ofp_print_flow_mod(string, msg, code, verbosity);
1690 case OFPUTIL_OFPT_PORT_MOD:
1691 ofp_print_port_mod(string, msg);
1694 case OFPUTIL_OFPT_BARRIER_REQUEST:
1695 case OFPUTIL_OFPT_BARRIER_REPLY:
1698 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1699 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1703 case OFPUTIL_OFPST_DESC_REQUEST:
1704 case OFPUTIL_OFPST_PORT_DESC_REQUEST:
1705 ofp_print_stats_request(string, oh);
1708 case OFPUTIL_OFPST_FLOW_REQUEST:
1709 case OFPUTIL_NXST_FLOW_REQUEST:
1710 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1711 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1712 ofp_print_stats_request(string, oh);
1713 ofp_print_flow_stats_request(string, msg);
1716 case OFPUTIL_OFPST_TABLE_REQUEST:
1717 ofp_print_stats_request(string, oh);
1720 case OFPUTIL_OFPST_PORT_REQUEST:
1721 ofp_print_stats_request(string, oh);
1722 ofp_print_ofpst_port_request(string, msg);
1725 case OFPUTIL_OFPST_QUEUE_REQUEST:
1726 ofp_print_stats_request(string, oh);
1727 ofp_print_ofpst_queue_request(string, msg);
1730 case OFPUTIL_OFPST_DESC_REPLY:
1731 ofp_print_stats_reply(string, oh);
1732 ofp_print_ofpst_desc_reply(string, msg);
1735 case OFPUTIL_OFPST_FLOW_REPLY:
1736 case OFPUTIL_NXST_FLOW_REPLY:
1737 ofp_print_stats_reply(string, oh);
1738 ofp_print_flow_stats_reply(string, oh);
1741 case OFPUTIL_OFPST_QUEUE_REPLY:
1742 ofp_print_stats_reply(string, oh);
1743 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1746 case OFPUTIL_OFPST_PORT_REPLY:
1747 ofp_print_stats_reply(string, oh);
1748 ofp_print_ofpst_port_reply(string, oh, verbosity);
1751 case OFPUTIL_OFPST_TABLE_REPLY:
1752 ofp_print_stats_reply(string, oh);
1753 ofp_print_ofpst_table_reply(string, oh, verbosity);
1756 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1757 ofp_print_stats_reply(string, oh);
1758 ofp_print_ofpst_aggregate_reply(string, msg);
1761 case OFPUTIL_OFPST_PORT_DESC_REPLY:
1762 ofp_print_stats_reply(string, oh);
1763 ofp_print_ofpst_port_desc_reply(string, oh);
1766 case OFPUTIL_NXT_ROLE_REQUEST:
1767 case OFPUTIL_NXT_ROLE_REPLY:
1768 ofp_print_nxt_role_message(string, msg);
1771 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1772 ofp_print_nxt_flow_mod_table_id(string, msg);
1775 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1776 ofp_print_nxt_set_flow_format(string, msg);
1779 case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
1780 ofp_print_nxt_set_packet_in_format(string, msg);
1783 case OFPUTIL_NXT_FLOW_AGE:
1786 case OFPUTIL_NXT_SET_CONTROLLER_ID:
1787 ofp_print_nxt_set_controller_id(string, msg);
1790 case OFPUTIL_NXT_SET_ASYNC_CONFIG:
1791 ofp_print_nxt_set_async_config(string, msg);
1794 case OFPUTIL_NXST_AGGREGATE_REPLY:
1795 ofp_print_stats_reply(string, oh);
1796 ofp_print_nxst_aggregate_reply(string, msg);
1801 /* Composes and returns a string representing the OpenFlow packet of 'len'
1802 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1803 * verbosity and higher numbers increase verbosity. The caller is responsible
1804 * for freeing the string. */
1806 ofp_to_string(const void *oh_, size_t len, int verbosity)
1808 struct ds string = DS_EMPTY_INITIALIZER;
1809 const struct ofp_header *oh = oh_;
1812 ds_put_cstr(&string, "OpenFlow message is empty\n");
1813 } else if (len < sizeof(struct ofp_header)) {
1814 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1816 } else if (ntohs(oh->length) > len) {
1817 ds_put_format(&string,
1818 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1819 len, ntohs(oh->length));
1820 } else if (ntohs(oh->length) < len) {
1821 ds_put_format(&string,
1822 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1823 ntohs(oh->length), len);
1825 const struct ofputil_msg_type *type;
1828 error = ofputil_decode_msg_type(oh, &type);
1830 ofp_to_string__(oh, type, &string, verbosity);
1831 if (verbosity >= 5) {
1832 if (ds_last(&string) != '\n') {
1833 ds_put_char(&string, '\n');
1835 ds_put_hex_dump(&string, oh, len, 0, true);
1837 if (ds_last(&string) != '\n') {
1838 ds_put_char(&string, '\n');
1840 return ds_steal_cstr(&string);
1843 ofp_print_error(&string, error);
1845 ds_put_hex_dump(&string, oh, len, 0, true);
1846 return ds_steal_cstr(&string);
1850 print_and_free(FILE *stream, char *string)
1852 fputs(string, stream);
1856 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1857 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1858 * numbers increase verbosity. */
1860 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1862 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1865 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1866 * 'data' to 'stream'. */
1868 ofp_print_packet(FILE *stream, const void *data, size_t len)
1870 print_and_free(stream, ofp_packet_to_string(data, len));