xref: /illumos-gate/usr/src/test/util-tests/tests/libsff/libsff_br.c (revision c093b3ec6d35e1fe023174ed7f6ca6b90690d526)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2017, Joyent, Inc.
14  */
15 
16 /*
17  * Print and tests SFF BR values.
18  */
19 
20 #include <stdio.h>
21 #include <errno.h>
22 #include <strings.h>
23 #include <err.h>
24 #include <libsff.h>
25 
26 /*
27  * Pick up private sff header file with offsets from lib/libsff.
28  */
29 #include "sff.h"
30 
31 int
32 main(void)
33 {
34 	int ret;
35 	uint8_t buf[256];
36 	nvlist_t *nvl;
37 	char *val;
38 
39 	/*
40 	 * SFF 8472 has two different modes of printing the bit rate. It has a
41 	 * nominal bit rate and then if 0xff is in that field it has a max and
42 	 * min.
43 	 */
44 	bzero(buf, sizeof (buf));
45 	buf[SFF_8472_BR_NOMINAL] = 0x42;
46 
47 	if ((ret = libsff_parse(buf, sizeof (buf), 0xa0, &nvl)) != 0) {
48 		errx(1, "TEST FAILED: failed to parse SFP compliance "
49 		    "values: %s\n", strerror(ret));
50 	}
51 
52 	if ((ret = nvlist_lookup_string(nvl, LIBSFF_KEY_BR_NOMINAL, &val)) !=
53 	    0) {
54 		errc(1, ret, "TEST FAILED: failed to find %s",
55 		    LIBSFF_KEY_BR_NOMINAL);
56 	}
57 	(void) printf("nominal: %s\n", val);
58 
59 	/*
60 	 * Make sure min, max are missing.
61 	 */
62 	if ((ret = nvlist_lookup_string(nvl, LIBSFF_KEY_BR_MIN, &val)) !=
63 	    ENOENT) {
64 		errc(1, ret, "TEST FAILED: found unexpected return value for "
65 		    "key %s: %d", LIBSFF_KEY_BR_MIN, ret);
66 	}
67 
68 	if ((ret = nvlist_lookup_string(nvl, LIBSFF_KEY_BR_MAX, &val)) !=
69 	    ENOENT) {
70 		errx(1, "TEST FAILED: found unexpected return value for key "
71 		    "%s: %d", LIBSFF_KEY_BR_MAX, ret);
72 	}
73 	nvlist_free(nvl);
74 
75 	/*
76 	 * Now the opposite.
77 	 */
78 	buf[SFF_8472_BR_NOMINAL] = 0xff;
79 	buf[SFF_8472_BR_MAX] = 0x50;
80 	buf[SFF_8472_BR_MIN] = 0x10;
81 
82 	if ((ret = libsff_parse(buf, sizeof (buf), 0xa0, &nvl)) != 0) {
83 		errc(1, ret, "TEST FAILED: failed to parse SFP compliance "
84 		    "values");
85 	}
86 
87 	if ((ret = nvlist_lookup_string(nvl, LIBSFF_KEY_BR_MAX, &val)) != 0) {
88 		errc(1, ret, "TEST FAILED: failed to find %s",
89 		    LIBSFF_KEY_BR_MAX);
90 	}
91 	(void) printf("max: %s\n", val);
92 
93 	if ((ret = nvlist_lookup_string(nvl, LIBSFF_KEY_BR_MIN, &val)) != 0) {
94 		errc(1, ret, "TEST FAILED: failed to find %s",
95 		    LIBSFF_KEY_BR_MIN);
96 	}
97 	(void) printf("min: %s\n", val);
98 
99 	if ((ret = nvlist_lookup_string(nvl, LIBSFF_KEY_BR_NOMINAL, &val)) !=
100 	    ENOENT) {
101 		errx(1, "TEST FALIED: found unexpected return value for key "
102 		    "%s: %d\n", LIBSFF_KEY_BR_NOMINAL, ret);
103 	}
104 	nvlist_free(nvl);
105 
106 	/*
107 	 * Now for QSFP+
108 	 */
109 	(void) puts("\n\nQSFP\n");
110 	bzero(buf, sizeof (buf));
111 	buf[SFF_8472_IDENTIFIER] = SFF_8024_ID_QSFP;
112 	buf[SFF_8636_BR_NOMINAL] = 0x42;
113 
114 	if ((ret = libsff_parse(buf, sizeof (buf), 0xa0, &nvl)) != 0) {
115 		errc(1, ret, "TEST FAILED: failed to parse QSFP BR "
116 		    "values");
117 	}
118 
119 	if ((ret = nvlist_lookup_string(nvl, LIBSFF_KEY_BR_NOMINAL,
120 	    &val)) != 0) {
121 		errc(1, ret, "TEST FAILED: failed to find %s",
122 		    LIBSFF_KEY_BR_NOMINAL);
123 	}
124 	(void) printf("nominal: %s\n", val);
125 
126 	nvlist_free(nvl);
127 
128 	return (0);
129 }
130