]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-print.c
5b840982fb775307279af0af65ce2f2b3014a2fb
[mirror_ovs.git] / lib / ofp-print.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 "netdev.h"
38 #include "nx-match.h"
39 #include "ofp-errors.h"
40 #include "ofp-util.h"
41 #include "ofpbuf.h"
42 #include "openflow/openflow.h"
43 #include "openflow/nicira-ext.h"
44 #include "packets.h"
45 #include "pcap.h"
46 #include "type-props.h"
47 #include "unaligned.h"
48 #include "util.h"
49
50 static void ofp_print_queue_name(struct ds *string, uint32_t port);
51 static void ofp_print_error(struct ds *, enum ofperr);
52
53
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.*/
56 char *
57 ofp_packet_to_string(const void *data, size_t len)
58 {
59 struct ds ds = DS_EMPTY_INITIALIZER;
60 struct ofpbuf buf;
61 struct flow flow;
62
63 ofpbuf_use_const(&buf, data, len);
64 flow_extract(&buf, 0, 0, 0, &flow);
65 flow_format(&ds, &flow);
66
67 if (buf.l7) {
68 if (flow.nw_proto == IPPROTO_TCP) {
69 struct tcp_header *th = buf.l4;
70 ds_put_format(&ds, " tcp_csum:%"PRIx16,
71 ntohs(th->tcp_csum));
72 } else if (flow.nw_proto == IPPROTO_UDP) {
73 struct udp_header *uh = buf.l4;
74 ds_put_format(&ds, " udp_csum:%"PRIx16,
75 ntohs(uh->udp_csum));
76 }
77 }
78
79 ds_put_char(&ds, '\n');
80
81 return ds_cstr(&ds);
82 }
83
84 static void
85 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
86 int verbosity)
87 {
88 struct ofputil_packet_in pin;
89 int error;
90 int i;
91
92 error = ofputil_decode_packet_in(&pin, oh);
93 if (error) {
94 ofp_print_error(string, error);
95 return;
96 }
97
98 if (pin.table_id) {
99 ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
100 }
101
102 if (pin.cookie) {
103 ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
104 }
105
106 ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
107 ofputil_format_port(pin.fmd.in_port, string);
108
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));
113 }
114 }
115
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]);
121 }
122 }
123 }
124
125 ds_put_format(string, " (via %s)",
126 ofputil_packet_in_reason_to_string(pin.reason));
127
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***)");
133 }
134 } else {
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***)");
138 }
139 }
140 ds_put_char(string, '\n');
141
142 if (verbosity > 0) {
143 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
144 ds_put_cstr(string, packet);
145 free(packet);
146 }
147 }
148
149 static void
150 print_note(struct ds *string, const struct nx_action_note *nan)
151 {
152 size_t len;
153 size_t i;
154
155 ds_put_cstr(string, "note:");
156 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
157 for (i = 0; i < len; i++) {
158 if (i) {
159 ds_put_char(string, '.');
160 }
161 ds_put_format(string, "%02"PRIx8, nan->note[i]);
162 }
163 }
164
165 static void
166 ofp_print_action(struct ds *s, const union ofp_action *a,
167 enum ofputil_action_code code)
168 {
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;
183 uint16_t port;
184
185 switch (code) {
186 case OFPUTIL_OFPAT10_OUTPUT:
187 port = ntohs(a->output.port);
188 if (port < OFPP_MAX) {
189 ds_put_format(s, "output:%"PRIu16, port);
190 } else {
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));
195 } else {
196 ds_put_cstr(s, ":all");
197 }
198 }
199 }
200 break;
201
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));
207 break;
208
209 case OFPUTIL_OFPAT10_SET_VLAN_VID:
210 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
211 ntohs(a->vlan_vid.vlan_vid));
212 break;
213
214 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
215 ds_put_format(s, "mod_vlan_pcp:%"PRIu8, a->vlan_pcp.vlan_pcp);
216 break;
217
218 case OFPUTIL_OFPAT10_STRIP_VLAN:
219 ds_put_cstr(s, "strip_vlan");
220 break;
221
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));
226 break;
227
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));
232 break;
233
234 case OFPUTIL_OFPAT10_SET_NW_SRC:
235 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
236 break;
237
238 case OFPUTIL_OFPAT10_SET_NW_DST:
239 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
240 break;
241
242 case OFPUTIL_OFPAT10_SET_NW_TOS:
243 ds_put_format(s, "mod_nw_tos:%d", a->nw_tos.nw_tos);
244 break;
245
246 case OFPUTIL_OFPAT10_SET_TP_SRC:
247 ds_put_format(s, "mod_tp_src:%d", ntohs(a->tp_port.tp_port));
248 break;
249
250 case OFPUTIL_OFPAT10_SET_TP_DST:
251 ds_put_format(s, "mod_tp_dst:%d", ntohs(a->tp_port.tp_port));
252 break;
253
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);
258 break;
259
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);
265 }
266 ds_put_char(s, ',');
267 if (nar->table != 255) {
268 ds_put_format(s, "%"PRIu8, nar->table);
269 }
270 ds_put_char(s, ')');
271 break;
272
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));
276 break;
277
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));
281 break;
282
283 case OFPUTIL_NXAST_POP_QUEUE:
284 ds_put_cstr(s, "pop_queue");
285 break;
286
287 case OFPUTIL_NXAST_NOTE:
288 print_note(s, (const struct nx_action_note *) a);
289 break;
290
291 case OFPUTIL_NXAST_REG_MOVE:
292 move = (const struct nx_action_reg_move *) a;
293 nxm_format_reg_move(move, s);
294 break;
295
296 case OFPUTIL_NXAST_REG_LOAD:
297 load = (const struct nx_action_reg_load *) a;
298 nxm_format_reg_load(load, s);
299 break;
300
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));
305 break;
306
307 case OFPUTIL_NXAST_MULTIPATH:
308 nam = (const struct nx_action_multipath *) a;
309 multipath_format(nam, s);
310 break;
311
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);
317 ds_put_char(s, ')');
318 break;
319
320 case OFPUTIL_NXAST_BUNDLE:
321 case OFPUTIL_NXAST_BUNDLE_LOAD:
322 bundle_format((const struct nx_action_bundle *) a, s);
323 break;
324
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);
330 break;
331
332 case OFPUTIL_NXAST_LEARN:
333 learn_format((const struct nx_action_learn *) a, s);
334 break;
335
336 case OFPUTIL_NXAST_DEC_TTL:
337 ds_put_cstr(s, "dec_ttl");
338 break;
339
340 case OFPUTIL_NXAST_EXIT:
341 ds_put_cstr(s, "exit");
342 break;
343
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));
350 }
351 if (naft->fin_hard_timeout) {
352 ds_put_format(s, "hard_timeout=%"PRIu16",",
353 ntohs(naft->fin_hard_timeout));
354 }
355 ds_chomp(s, ',');
356 ds_put_char(s, ')');
357 break;
358
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));
365 }
366 if (nac->max_len != htons(UINT16_MAX)) {
367 ds_put_format(s, "max_len=%"PRIu16",", ntohs(nac->max_len));
368 }
369 if (nac->controller_id != htons(0)) {
370 ds_put_format(s, "id=%"PRIu16",", ntohs(nac->controller_id));
371 }
372 ds_chomp(s, ',');
373 ds_put_char(s, ')');
374 break;
375
376 default:
377 break;
378 }
379 }
380
381 void
382 ofp_print_actions(struct ds *string, const union ofp_action *actions,
383 size_t n_actions)
384 {
385 const union ofp_action *a;
386 size_t left;
387
388 ds_put_cstr(string, "actions=");
389 if (!n_actions) {
390 ds_put_cstr(string, "drop");
391 }
392
393 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
394 int code = ofputil_decode_action(a);
395 if (code >= 0) {
396 if (a != actions) {
397 ds_put_cstr(string, ",");
398 }
399 ofp_print_action(string, a, code);
400 } else {
401 ofp_print_error(string, -code);
402 }
403 }
404 if (left > 0) {
405 ds_put_format(string, " ***%zu leftover bytes following actions",
406 left * sizeof *a);
407 }
408 }
409
410 static void
411 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
412 int verbosity)
413 {
414 struct ofputil_packet_out po;
415 enum ofperr error;
416
417 error = ofputil_decode_packet_out(&po, opo);
418 if (error) {
419 ofp_print_error(string, error);
420 return;
421 }
422
423 ds_put_cstr(string, " in_port=");
424 ofputil_format_port(po.in_port, string);
425
426 ds_put_char(string, ' ');
427 ofp_print_actions(string, po.actions, po.n_actions);
428
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);
435 free(packet);
436 }
437 } else {
438 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
439 }
440 ds_put_char(string, '\n');
441 }
442
443 /* qsort comparison function. */
444 static int
445 compare_ports(const void *a_, const void *b_)
446 {
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;
451
452 return ap < bp ? -1 : ap > bp;
453 }
454
455 static void
456 ofp_print_bit_names(struct ds *string, uint32_t bits,
457 const char *(*bit_to_name)(uint32_t bit))
458 {
459 int n = 0;
460 int i;
461
462 if (!bits) {
463 ds_put_cstr(string, "0");
464 return;
465 }
466
467 for (i = 0; i < 32; i++) {
468 uint32_t bit = UINT32_C(1) << i;
469
470 if (bits & bit) {
471 const char *name = bit_to_name(bit);
472 if (name) {
473 if (n++) {
474 ds_put_char(string, ' ');
475 }
476 ds_put_cstr(string, name);
477 bits &= ~bit;
478 }
479 }
480 }
481
482 if (bits) {
483 if (n++) {
484 ds_put_char(string, ' ');
485 }
486 ds_put_format(string, "0x%"PRIx32, bits);
487 }
488 }
489
490 static const char *
491 netdev_feature_to_name(uint32_t bit)
492 {
493 enum netdev_features f = bit;
494
495 switch (f) {
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";
512 }
513
514 return NULL;
515 }
516
517 static void
518 ofp_print_port_features(struct ds *string, enum netdev_features features)
519 {
520 ofp_print_bit_names(string, features, netdev_feature_to_name);
521 ds_put_char(string, '\n');
522 }
523
524 static const char *
525 ofputil_port_config_to_name(uint32_t bit)
526 {
527 enum ofputil_port_config pc = bit;
528
529 switch (pc) {
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";
537 }
538
539 return NULL;
540 }
541
542 static void
543 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
544 {
545 ofp_print_bit_names(string, config, ofputil_port_config_to_name);
546 ds_put_char(string, '\n');
547 }
548
549 static const char *
550 ofputil_port_state_to_name(uint32_t bit)
551 {
552 enum ofputil_port_state ps = bit;
553
554 switch (ps) {
555 case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
556 case OFPUTIL_PS_BLOCKED: return "BLOCKED";
557 case OFPUTIL_PS_LIVE: return "LIVE";
558
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. */
564 return NULL;
565 }
566
567 return NULL;
568 }
569
570 static void
571 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
572 {
573 enum ofputil_port_state stp_state;
574
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.
577 *
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;
583 if (stp_state) {
584 ds_put_cstr(string,
585 (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
586 : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
587 : "STP_BLOCK"));
588 state &= ~OFPUTIL_PS_STP_MASK;
589 if (state) {
590 ofp_print_bit_names(string, state, ofputil_port_state_to_name);
591 }
592 } else {
593 ofp_print_bit_names(string, state, ofputil_port_state_to_name);
594 }
595 ds_put_char(string, '\n');
596 }
597
598 static void
599 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
600 {
601 char name[sizeof port->name];
602 int j;
603
604 memcpy(name, port->name, sizeof name);
605 for (j = 0; j < sizeof name - 1; j++) {
606 if (!isprint((unsigned char) name[j])) {
607 break;
608 }
609 }
610 name[j] = '\0';
611
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));
616
617 ds_put_cstr(string, " config: ");
618 ofp_print_port_config(string, port->config);
619
620 ds_put_cstr(string, " state: ");
621 ofp_print_port_state(string, port->state);
622
623 if (port->curr) {
624 ds_put_format(string, " current: ");
625 ofp_print_port_features(string, port->curr);
626 }
627 if (port->advertised) {
628 ds_put_format(string, " advertised: ");
629 ofp_print_port_features(string, port->advertised);
630 }
631 if (port->supported) {
632 ds_put_format(string, " supported: ");
633 ofp_print_port_features(string, port->supported);
634 }
635 if (port->peer) {
636 ds_put_format(string, " peer: ");
637 ofp_print_port_features(string, port->peer);
638 }
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));
643 }
644
645 static const char *
646 ofputil_capabilities_to_name(uint32_t bit)
647 {
648 enum ofputil_capabilities capabilities = bit;
649
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";
659 }
660
661 return NULL;
662 }
663
664 static const char *
665 ofputil_action_bitmap_to_name(uint32_t bit)
666 {
667 enum ofputil_action_bitmap action = bit;
668
669 switch (action) {
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";
697 }
698
699 return NULL;
700 }
701
702 static void
703 ofp_print_switch_features(struct ds *string,
704 const struct ofp_switch_features *osf)
705 {
706 struct ofputil_switch_features features;
707 struct ofputil_phy_port *ports;
708 enum ofperr error;
709 struct ofpbuf b;
710 size_t n_ports;
711 size_t i;
712
713 error = ofputil_decode_switch_features(osf, &features, &b);
714 if (error) {
715 ofp_print_error(string, error);
716 return;
717 }
718
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);
722
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');
727
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');
732
733 n_ports = ofputil_count_phy_ports(osf);
734
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]);
738 if (error) {
739 ofp_print_error(string, error);
740 return;
741 }
742 }
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]);
746 }
747 free(ports);
748 }
749
750 static void
751 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
752 {
753 enum ofp_config_flags flags;
754
755 flags = ntohs(osc->flags);
756
757 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
758 flags &= ~OFPC_FRAG_MASK;
759
760 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
761 ds_put_format(string, " invalid_ttl_to_controller");
762 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
763 }
764
765 if (flags) {
766 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
767 }
768
769 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
770 }
771
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)));
775
776 static void print_wild(struct ds *string, const char *leader, int is_wild,
777 int verbosity, const char *format, ...)
778 {
779 if (is_wild && verbosity < 2) {
780 return;
781 }
782 ds_put_cstr(string, leader);
783 if (!is_wild) {
784 va_list args;
785
786 va_start(args, format);
787 ds_put_format_valist(string, format, args);
788 va_end(args);
789 } else {
790 ds_put_char(string, '*');
791 }
792 ds_put_char(string, ',');
793 }
794
795 static void
796 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
797 uint32_t wild_bits, int verbosity)
798 {
799 if (wild_bits >= 32 && verbosity < 2) {
800 return;
801 }
802 ds_put_cstr(string, leader);
803 if (wild_bits < 32) {
804 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
805 if (wild_bits) {
806 ds_put_format(string, "/%d", 32 - wild_bits);
807 }
808 } else {
809 ds_put_char(string, '*');
810 }
811 ds_put_char(string, ',');
812 }
813
814 void
815 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
816 {
817 char *s = ofp_match_to_string(om, verbosity);
818 ds_put_cstr(f, s);
819 free(s);
820 }
821
822 char *
823 ofp_match_to_string(const struct ofp_match *om, int verbosity)
824 {
825 struct ds f = DS_EMPTY_INITIALIZER;
826 uint32_t w = ntohl(om->wildcards);
827 bool skip_type = false;
828 bool skip_proto = false;
829
830 if (!(w & OFPFW_DL_TYPE)) {
831 skip_type = true;
832 if (om->dl_type == htons(ETH_TYPE_IP)) {
833 if (!(w & OFPFW_NW_PROTO)) {
834 skip_proto = true;
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,");
841 } else {
842 ds_put_cstr(&f, "ip,");
843 skip_proto = false;
844 }
845 } else {
846 ds_put_cstr(&f, "ip,");
847 }
848 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
849 ds_put_cstr(&f, "arp,");
850 } else {
851 skip_type = false;
852 }
853 }
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));
864 if (!skip_type) {
865 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
866 "0x%04x", ntohs(om->dl_type));
867 }
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);
872 if (!skip_proto) {
873 if (om->dl_type == htons(ETH_TYPE_ARP)) {
874 print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
875 "%u", om->nw_proto);
876 } else {
877 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
878 "%u", om->nw_proto);
879 }
880 }
881 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
882 "%u", om->nw_tos);
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));
888 } else {
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));
893 }
894 if (ds_last(&f) == ',') {
895 f.length--;
896 }
897 return ds_cstr(&f);
898 }
899
900 static void
901 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
902 enum ofputil_msg_code code, int verbosity)
903 {
904 struct ofputil_flow_mod fm;
905 bool need_priority;
906 enum ofperr error;
907
908 error = ofputil_decode_flow_mod(&fm, oh, OFPUTIL_P_OF10_TID);
909 if (error) {
910 ofp_print_error(s, error);
911 return;
912 }
913
914 ds_put_char(s, ' ');
915 switch (fm.command) {
916 case OFPFC_ADD:
917 ds_put_cstr(s, "ADD");
918 break;
919 case OFPFC_MODIFY:
920 ds_put_cstr(s, "MOD");
921 break;
922 case OFPFC_MODIFY_STRICT:
923 ds_put_cstr(s, "MOD_STRICT");
924 break;
925 case OFPFC_DELETE:
926 ds_put_cstr(s, "DEL");
927 break;
928 case OFPFC_DELETE_STRICT:
929 ds_put_cstr(s, "DEL_STRICT");
930 break;
931 default:
932 ds_put_format(s, "cmd:%d", fm.command);
933 }
934 if (fm.table_id != 0) {
935 ds_put_format(s, " table:%d", fm.table_id);
936 }
937
938 ds_put_char(s, ' ');
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);
942
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;
948 char *nxm_s;
949
950 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
951 ds_put_cstr(s, nxm_s);
952 free(nxm_s);
953
954 /* nx_match_to_string() doesn't print priority. */
955 need_priority = true;
956 } else {
957 cls_rule_format(&fm.cr, s);
958
959 /* cls_rule_format() does print priority. */
960 need_priority = false;
961 }
962
963 if (ds_last(s) != ' ') {
964 ds_put_char(s, ' ');
965 }
966 if (fm.cookie != htonll(0)) {
967 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
968 }
969 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
970 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
971 }
972 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
973 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
974 }
975 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
976 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
977 }
978 if (fm.buffer_id != UINT32_MAX) {
979 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
980 }
981 if (fm.flags != 0) {
982 uint16_t flags = fm.flags;
983
984 if (flags & OFPFF_SEND_FLOW_REM) {
985 ds_put_cstr(s, "send_flow_rem ");
986 }
987 if (flags & OFPFF_CHECK_OVERLAP) {
988 ds_put_cstr(s, "check_overlap ");
989 }
990 if (flags & OFPFF_EMERG) {
991 ds_put_cstr(s, "emerg ");
992 }
993
994 flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP | OFPFF_EMERG);
995 if (flags) {
996 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
997 }
998 }
999
1000 ofp_print_actions(s, fm.actions, fm.n_actions);
1001 }
1002
1003 static void
1004 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
1005 {
1006 ds_put_format(string, "%u", sec);
1007 if (nsec > 0) {
1008 ds_put_format(string, ".%09u", nsec);
1009 while (string->string[string->length - 1] == '0') {
1010 string->length--;
1011 }
1012 }
1013 ds_put_char(string, 's');
1014 }
1015
1016 static const char *
1017 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
1018 {
1019 static char s[32];
1020
1021 switch (reason) {
1022 case OFPRR_IDLE_TIMEOUT:
1023 return "idle";
1024 case OFPRR_HARD_TIMEOUT:
1025 return "hard";
1026 case OFPRR_DELETE:
1027 return "delete";
1028 case OFPRR_GROUP_DELETE:
1029 return "group_delete";
1030 default:
1031 sprintf(s, "%d", (int) reason);
1032 return s;
1033 }
1034 }
1035
1036 static void
1037 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
1038 {
1039 struct ofputil_flow_removed fr;
1040 enum ofperr error;
1041
1042 error = ofputil_decode_flow_removed(&fr, oh);
1043 if (error) {
1044 ofp_print_error(string, error);
1045 return;
1046 }
1047
1048 ds_put_char(string, ' ');
1049 cls_rule_format(&fr.rule, string);
1050
1051 ds_put_format(string, " reason=%s",
1052 ofp_flow_removed_reason_to_string(fr.reason));
1053
1054 if (fr.cookie != htonll(0)) {
1055 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
1056 }
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);
1061 }
1062
1063 static void
1064 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
1065 {
1066 struct ofputil_port_mod pm;
1067 enum ofperr error;
1068
1069 error = ofputil_decode_port_mod(oh, &pm);
1070 if (error) {
1071 ofp_print_error(string, error);
1072 return;
1073 }
1074
1075 ds_put_format(string, "port: %"PRIu16": addr:"ETH_ADDR_FMT"\n",
1076 pm.port_no, ETH_ADDR_ARGS(pm.hw_addr));
1077
1078 ds_put_format(string, " config: ");
1079 ofp_print_port_config(string, pm.config);
1080
1081 ds_put_format(string, " mask: ");
1082 ofp_print_port_config(string, pm.mask);
1083
1084 ds_put_format(string, " advertise: ");
1085 if (pm.advertise) {
1086 ofp_print_port_features(string, pm.advertise);
1087 } else {
1088 ds_put_format(string, "UNCHANGED\n");
1089 }
1090 }
1091
1092 static void
1093 ofp_print_error(struct ds *string, enum ofperr error)
1094 {
1095 if (string->length) {
1096 ds_put_char(string, ' ');
1097 }
1098 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1099 }
1100
1101 static void
1102 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1103 {
1104 size_t len = ntohs(oem->header.length);
1105 size_t payload_ofs, payload_len;
1106 const void *payload;
1107 enum ofperr error;
1108 char *s;
1109
1110 error = ofperr_decode_msg(&oem->header, &payload_ofs);
1111 if (!error) {
1112 ds_put_cstr(string, "***decode error***");
1113 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1114 return;
1115 }
1116
1117 ds_put_format(string, " %s\n", ofperr_get_name(error));
1118
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);
1123 } else {
1124 s = ofp_to_string(payload, payload_len, 1);
1125 ds_put_cstr(string, s);
1126 free(s);
1127 }
1128 }
1129
1130 static void
1131 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1132 {
1133 struct ofputil_port_status ps;
1134 enum ofperr error;
1135
1136 error = ofputil_decode_port_status(ops, &ps);
1137 if (error) {
1138 ofp_print_error(string, error);
1139 return;
1140 }
1141
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:");
1148 }
1149
1150 ofp_print_phy_port(string, &ps.desc);
1151 }
1152
1153 static void
1154 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
1155 {
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);
1167 }
1168
1169 static void
1170 ofp_print_flow_stats_request(struct ds *string,
1171 const struct ofp_stats_msg *osm)
1172 {
1173 struct ofputil_flow_stats_request fsr;
1174 enum ofperr error;
1175
1176 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
1177 if (error) {
1178 ofp_print_error(string, error);
1179 return;
1180 }
1181
1182 if (fsr.table_id != 0xff) {
1183 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1184 }
1185
1186 if (fsr.out_port != OFPP_NONE) {
1187 ds_put_cstr(string, " out_port=");
1188 ofputil_format_port(fsr.out_port, string);
1189 }
1190
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;
1194
1195 ds_put_char(string, ' ');
1196 cls_rule_format(&fsr.match, string);
1197 }
1198
1199 static void
1200 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1201 {
1202 struct ofpbuf b;
1203
1204 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1205 for (;;) {
1206 struct ofputil_flow_stats fs;
1207 int retval;
1208
1209 retval = ofputil_decode_flow_stats_reply(&fs, &b, true);
1210 if (retval) {
1211 if (retval != EOF) {
1212 ds_put_cstr(string, " ***parse error***");
1213 }
1214 break;
1215 }
1216
1217 ds_put_char(string, '\n');
1218
1219 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1220 ntohll(fs.cookie));
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);
1227 }
1228 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1229 ds_put_format(string, "hard_timeout=%"PRIu16", ", fs.hard_timeout);
1230 }
1231 if (fs.idle_age >= 0) {
1232 ds_put_format(string, "idle_age=%d, ", fs.idle_age);
1233 }
1234 if (fs.hard_age >= 0 && fs.hard_age != fs.duration_sec) {
1235 ds_put_format(string, "hard_age=%d, ", fs.hard_age);
1236 }
1237
1238 cls_rule_format(&fs.rule, string);
1239 if (string->string[string->length - 1] != ' ') {
1240 ds_put_char(string, ' ');
1241 }
1242 ofp_print_actions(string, fs.actions, fs.n_actions);
1243 }
1244 }
1245
1246 static void
1247 ofp_print_ofpst_aggregate_reply(struct ds *string,
1248 const struct ofp_aggregate_stats_reply *asr)
1249 {
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));
1255 }
1256
1257 static void
1258 ofp_print_nxst_aggregate_reply(struct ds *string,
1259 const struct nx_aggregate_stats_reply *nasr)
1260 {
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));
1264 }
1265
1266 static void print_port_stat(struct ds *string, const char *leader,
1267 const ovs_32aligned_be64 *statp, int more)
1268 {
1269 uint64_t stat = ntohll(get_32aligned_be64(statp));
1270
1271 ds_put_cstr(string, leader);
1272 if (stat != UINT64_MAX) {
1273 ds_put_format(string, "%"PRIu64, stat);
1274 } else {
1275 ds_put_char(string, '?');
1276 }
1277 if (more) {
1278 ds_put_cstr(string, ", ");
1279 } else {
1280 ds_put_cstr(string, "\n");
1281 }
1282 }
1283
1284 static void
1285 ofp_print_ofpst_port_request(struct ds *string,
1286 const struct ofp_port_stats_request *psr)
1287 {
1288 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1289 }
1290
1291 static void
1292 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1293 int verbosity)
1294 {
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) {
1299 return;
1300 }
1301
1302 for (; n--; ps++) {
1303 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1304
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);
1313
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);
1320 }
1321 }
1322
1323 static void
1324 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1325 int verbosity)
1326 {
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) {
1331 return;
1332 }
1333
1334 for (; n--; ts++) {
1335 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1336 ovs_strlcpy(name, ts->name, sizeof name);
1337
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)));
1347 }
1348 }
1349
1350 static void
1351 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1352 {
1353 if (queue_id == OFPQ_ALL) {
1354 ds_put_cstr(string, "ALL");
1355 } else {
1356 ds_put_format(string, "%"PRIu32, queue_id);
1357 }
1358 }
1359
1360 static void
1361 ofp_print_ofpst_queue_request(struct ds *string,
1362 const struct ofp_queue_stats_request *qsr)
1363 {
1364 ds_put_cstr(string, "port=");
1365 ofputil_format_port(ntohs(qsr->port_no), string);
1366
1367 ds_put_cstr(string, " queue=");
1368 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1369 }
1370
1371 static void
1372 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1373 int verbosity)
1374 {
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) {
1379 return;
1380 }
1381
1382 for (; n--; qs++) {
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, ": ");
1388
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);
1392 }
1393 }
1394
1395 static void
1396 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1397 {
1398 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1399
1400 if (srq->flags) {
1401 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1402 ntohs(srq->flags));
1403 }
1404 }
1405
1406 static void
1407 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1408 {
1409 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1410
1411 if (srp->flags) {
1412 uint16_t flags = ntohs(srp->flags);
1413
1414 ds_put_cstr(string, " flags=");
1415 if (flags & OFPSF_REPLY_MORE) {
1416 ds_put_cstr(string, "[more]");
1417 flags &= ~OFPSF_REPLY_MORE;
1418 }
1419 if (flags) {
1420 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1421 flags);
1422 }
1423 }
1424 }
1425
1426 static void
1427 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1428 {
1429 size_t len = ntohs(oh->length);
1430
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);
1434 }
1435 }
1436
1437 static void
1438 ofp_print_nxt_role_message(struct ds *string,
1439 const struct nx_role_request *nrr)
1440 {
1441 unsigned int role = ntohl(nrr->role);
1442
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");
1450 } else {
1451 ds_put_format(string, "%u", role);
1452 }
1453 }
1454
1455 static void
1456 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1457 const struct nx_flow_mod_table_id *nfmti)
1458 {
1459 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1460 }
1461
1462 static void
1463 ofp_print_nxt_set_flow_format(struct ds *string,
1464 const struct nx_set_flow_format *nsff)
1465 {
1466 uint32_t format = ntohl(nsff->format);
1467
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));
1471 } else {
1472 ds_put_format(string, "%"PRIu32, format);
1473 }
1474 }
1475
1476 static void
1477 ofp_print_nxt_set_packet_in_format(struct ds *string,
1478 const struct nx_set_packet_in_format *nspf)
1479 {
1480 uint32_t format = ntohl(nspf->format);
1481
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));
1485 } else {
1486 ds_put_format(string, "%"PRIu32, format);
1487 }
1488 }
1489
1490 static const char *
1491 ofp_port_reason_to_string(enum ofp_port_reason reason)
1492 {
1493 static char s[32];
1494
1495 switch (reason) {
1496 case OFPPR_ADD:
1497 return "add";
1498
1499 case OFPPR_DELETE:
1500 return "delete";
1501
1502 case OFPPR_MODIFY:
1503 return "modify";
1504
1505 default:
1506 sprintf(s, "%d", (int) reason);
1507 return s;
1508 }
1509 }
1510
1511 static void
1512 ofp_print_nxt_set_async_config(struct ds *string,
1513 const struct nx_async_config *nac)
1514 {
1515 int i;
1516
1517 for (i = 0; i < 2; i++) {
1518 int j;
1519
1520 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1521
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));
1527 }
1528 }
1529 if (!nac->packet_in_mask[i]) {
1530 ds_put_cstr(string, " (off)");
1531 }
1532 ds_put_char(string, '\n');
1533
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));
1538 }
1539 }
1540 if (!nac->port_status_mask[i]) {
1541 ds_put_cstr(string, " (off)");
1542 }
1543 ds_put_char(string, '\n');
1544
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));
1550 }
1551 }
1552 if (!nac->flow_removed_mask[i]) {
1553 ds_put_cstr(string, " (off)");
1554 }
1555 ds_put_char(string, '\n');
1556 }
1557 }
1558
1559 static void
1560 ofp_print_nxt_set_controller_id(struct ds *string,
1561 const struct nx_controller_id *nci)
1562 {
1563 ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1564 }
1565
1566 static void
1567 ofp_to_string__(const struct ofp_header *oh,
1568 const struct ofputil_msg_type *type, struct ds *string,
1569 int verbosity)
1570 {
1571 enum ofputil_msg_code code;
1572 const void *msg = oh;
1573
1574 ds_put_cstr(string, ofputil_msg_type_name(type));
1575 switch (oh->version) {
1576 case OFP10_VERSION:
1577 break;
1578 case OFP11_VERSION:
1579 ds_put_cstr(string, " (OF1.1)");
1580 break;
1581 default:
1582 ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1583 break;
1584 }
1585 ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1586
1587 code = ofputil_msg_type_code(type);
1588 switch (code) {
1589 case OFPUTIL_MSG_INVALID:
1590 break;
1591
1592 case OFPUTIL_OFPT_HELLO:
1593 ds_put_char(string, '\n');
1594 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1595 0, true);
1596 break;
1597
1598 case OFPUTIL_OFPT_ERROR:
1599 ofp_print_error_msg(string, msg);
1600 break;
1601
1602 case OFPUTIL_OFPT_ECHO_REQUEST:
1603 case OFPUTIL_OFPT_ECHO_REPLY:
1604 ofp_print_echo(string, oh, verbosity);
1605 break;
1606
1607 case OFPUTIL_OFPT_FEATURES_REQUEST:
1608 break;
1609
1610 case OFPUTIL_OFPT_FEATURES_REPLY:
1611 ofp_print_switch_features(string, msg);
1612 break;
1613
1614 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1615 break;
1616
1617 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1618 case OFPUTIL_OFPT_SET_CONFIG:
1619 ofp_print_switch_config(string, msg);
1620 break;
1621
1622 case OFPUTIL_OFPT_PACKET_IN:
1623 case OFPUTIL_NXT_PACKET_IN:
1624 ofp_print_packet_in(string, msg, verbosity);
1625 break;
1626
1627 case OFPUTIL_OFPT_FLOW_REMOVED:
1628 case OFPUTIL_NXT_FLOW_REMOVED:
1629 ofp_print_flow_removed(string, msg);
1630 break;
1631
1632 case OFPUTIL_OFPT_PORT_STATUS:
1633 ofp_print_port_status(string, msg);
1634 break;
1635
1636 case OFPUTIL_OFPT_PACKET_OUT:
1637 ofp_print_packet_out(string, msg, verbosity);
1638 break;
1639
1640 case OFPUTIL_OFPT_FLOW_MOD:
1641 case OFPUTIL_NXT_FLOW_MOD:
1642 ofp_print_flow_mod(string, msg, code, verbosity);
1643 break;
1644
1645 case OFPUTIL_OFPT_PORT_MOD:
1646 ofp_print_port_mod(string, msg);
1647 break;
1648
1649 case OFPUTIL_OFPT_BARRIER_REQUEST:
1650 case OFPUTIL_OFPT_BARRIER_REPLY:
1651 break;
1652
1653 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1654 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1655 /* XXX */
1656 break;
1657
1658 case OFPUTIL_OFPST_DESC_REQUEST:
1659 ofp_print_stats_request(string, oh);
1660 break;
1661
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);
1668 break;
1669
1670 case OFPUTIL_OFPST_TABLE_REQUEST:
1671 ofp_print_stats_request(string, oh);
1672 break;
1673
1674 case OFPUTIL_OFPST_PORT_REQUEST:
1675 ofp_print_stats_request(string, oh);
1676 ofp_print_ofpst_port_request(string, msg);
1677 break;
1678
1679 case OFPUTIL_OFPST_QUEUE_REQUEST:
1680 ofp_print_stats_request(string, oh);
1681 ofp_print_ofpst_queue_request(string, msg);
1682 break;
1683
1684 case OFPUTIL_OFPST_DESC_REPLY:
1685 ofp_print_stats_reply(string, oh);
1686 ofp_print_ofpst_desc_reply(string, msg);
1687 break;
1688
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);
1693 break;
1694
1695 case OFPUTIL_OFPST_QUEUE_REPLY:
1696 ofp_print_stats_reply(string, oh);
1697 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1698 break;
1699
1700 case OFPUTIL_OFPST_PORT_REPLY:
1701 ofp_print_stats_reply(string, oh);
1702 ofp_print_ofpst_port_reply(string, oh, verbosity);
1703 break;
1704
1705 case OFPUTIL_OFPST_TABLE_REPLY:
1706 ofp_print_stats_reply(string, oh);
1707 ofp_print_ofpst_table_reply(string, oh, verbosity);
1708 break;
1709
1710 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1711 ofp_print_stats_reply(string, oh);
1712 ofp_print_ofpst_aggregate_reply(string, msg);
1713 break;
1714
1715 case OFPUTIL_NXT_ROLE_REQUEST:
1716 case OFPUTIL_NXT_ROLE_REPLY:
1717 ofp_print_nxt_role_message(string, msg);
1718 break;
1719
1720 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1721 ofp_print_nxt_flow_mod_table_id(string, msg);
1722 break;
1723
1724 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1725 ofp_print_nxt_set_flow_format(string, msg);
1726 break;
1727
1728 case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
1729 ofp_print_nxt_set_packet_in_format(string, msg);
1730 break;
1731
1732 case OFPUTIL_NXT_FLOW_AGE:
1733 break;
1734
1735 case OFPUTIL_NXT_SET_CONTROLLER_ID:
1736 ofp_print_nxt_set_controller_id(string, msg);
1737 break;
1738
1739 case OFPUTIL_NXT_SET_ASYNC_CONFIG:
1740 ofp_print_nxt_set_async_config(string, msg);
1741 break;
1742
1743 case OFPUTIL_NXST_AGGREGATE_REPLY:
1744 ofp_print_stats_reply(string, oh);
1745 ofp_print_nxst_aggregate_reply(string, msg);
1746 break;
1747 }
1748 }
1749
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. */
1754 char *
1755 ofp_to_string(const void *oh_, size_t len, int verbosity)
1756 {
1757 struct ds string = DS_EMPTY_INITIALIZER;
1758 const struct ofp_header *oh = oh_;
1759
1760 if (!len) {
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",
1764 len);
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);
1773 } else {
1774 const struct ofputil_msg_type *type;
1775 enum ofperr error;
1776
1777 error = ofputil_decode_msg_type(oh, &type);
1778 if (!error) {
1779 ofp_to_string__(oh, type, &string, verbosity);
1780 if (verbosity >= 5) {
1781 if (ds_last(&string) != '\n') {
1782 ds_put_char(&string, '\n');
1783 }
1784 ds_put_hex_dump(&string, oh, len, 0, true);
1785 }
1786 if (ds_last(&string) != '\n') {
1787 ds_put_char(&string, '\n');
1788 }
1789 return ds_steal_cstr(&string);
1790 }
1791
1792 ofp_print_error(&string, error);
1793 }
1794 ds_put_hex_dump(&string, oh, len, 0, true);
1795 return ds_steal_cstr(&string);
1796 }
1797 \f
1798 static void
1799 print_and_free(FILE *stream, char *string)
1800 {
1801 fputs(string, stream);
1802 free(string);
1803 }
1804
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. */
1808 void
1809 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1810 {
1811 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1812 }
1813
1814 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1815 * 'data' to 'stream'. */
1816 void
1817 ofp_print_packet(FILE *stream, const void *data, size_t len)
1818 {
1819 print_and_free(stream, ofp_packet_to_string(data, len));
1820 }