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