xref: /freebsd/sbin/ifconfig/sfp.c (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
1 /*-
2  * Copyright (c) 2014 Alexander V. Chernikov. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
30 
31 #include <net/if.h>
32 #include <net/sff8436.h>
33 #include <net/sff8472.h>
34 
35 #include <math.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdbool.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include <libutil.h>
46 
47 #include <libifconfig.h>
48 #include <libifconfig_sfp.h>
49 
50 #include "ifconfig.h"
51 
52 void
53 sfp_status(if_ctx *ctx)
54 {
55 	struct ifconfig_sfp_info info;
56 	struct ifconfig_sfp_info_strings strings;
57 	struct ifconfig_sfp_vendor_info vendor_info;
58 	struct ifconfig_sfp_status status;
59 	size_t channel_count;
60 	int verbose = ctx->args->verbose;
61 
62 	if (ifconfig_sfp_get_sfp_info(lifh, ctx->ifname, &info) == -1)
63 		return;
64 
65 	ifconfig_sfp_get_sfp_info_strings(&info, &strings);
66 
67 	printf("\tplugged: %s %s (%s)\n",
68 	    ifconfig_sfp_id_display(info.sfp_id),
69 	    ifconfig_sfp_physical_spec(&info, &strings),
70 	    strings.sfp_conn);
71 
72 	if (ifconfig_sfp_get_sfp_vendor_info(lifh, ctx->ifname, &vendor_info) == -1)
73 		return;
74 
75 	printf("\tvendor: %s PN: %s SN: %s DATE: %s\n",
76 	    vendor_info.name, vendor_info.pn, vendor_info.sn, vendor_info.date);
77 
78 	if (ifconfig_sfp_id_is_qsfp(info.sfp_id)) {
79 		if (verbose > 1)
80 			printf("\tcompliance level: %s\n", strings.sfp_rev);
81 	} else {
82 		if (verbose > 5) {
83 			printf("Class: %s\n",
84 			    ifconfig_sfp_physical_spec(&info, &strings));
85 			printf("Length: %s\n", strings.sfp_fc_len);
86 			printf("Tech: %s\n", strings.sfp_cab_tech);
87 			printf("Media: %s\n", strings.sfp_fc_media);
88 			printf("Speed: %s\n", strings.sfp_fc_speed);
89 		}
90 	}
91 
92 	if (ifconfig_sfp_get_sfp_status(lifh, ctx->ifname, &status) == 0) {
93 		if (ifconfig_sfp_id_is_qsfp(info.sfp_id) && verbose > 1)
94 			printf("\tnominal bitrate: %u Mbps\n", status.bitrate);
95 		printf("\tmodule temperature: %.2f C voltage: %.2f Volts\n",
96 		    status.temp, status.voltage);
97 		channel_count = ifconfig_sfp_channel_count(&info);
98 		for (size_t chan = 0; chan < channel_count; ++chan) {
99 			uint16_t rx = status.channel[chan].rx;
100 			uint16_t tx = status.channel[chan].tx;
101 			printf("\tlane %zu: "
102 			    "RX power: %.2f mW (%.2f dBm) TX bias: %.2f mA\n",
103 			    chan + 1, power_mW(rx), power_dBm(rx), bias_mA(tx));
104 		}
105 		ifconfig_sfp_free_sfp_status(&status);
106 	}
107 
108 	if (verbose > 2) {
109 		struct ifconfig_sfp_dump dump;
110 
111 		if (ifconfig_sfp_get_sfp_dump(lifh, ctx->ifname, &dump) == -1)
112 			return;
113 
114 		if (ifconfig_sfp_id_is_qsfp(info.sfp_id)) {
115 			printf("\n\tSFF8436 DUMP (0xA0 128..255 range):\n");
116 			hexdump(dump.data + QSFP_DUMP1_START, QSFP_DUMP1_SIZE,
117 			    "\t", HD_OMIT_COUNT | HD_OMIT_CHARS);
118 			printf("\n\tSFF8436 DUMP (0xA0 0..81 range):\n");
119 			hexdump(dump.data + QSFP_DUMP0_START, QSFP_DUMP0_SIZE,
120 			    "\t", HD_OMIT_COUNT | HD_OMIT_CHARS);
121 		} else {
122 			printf("\n\tSFF8472 DUMP (0xA0 0..127 range):\n");
123 			hexdump(dump.data + SFP_DUMP_START, SFP_DUMP_SIZE,
124 			    "\t", HD_OMIT_COUNT | HD_OMIT_CHARS);
125 		}
126 	}
127 }
128