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 #ifndef lint 27 static const char rcsid[] = 28 "$FreeBSD$"; 29 #endif /* not lint */ 30 31 #include <sys/types.h> 32 #include <sys/param.h> 33 #include <sys/ioctl.h> 34 #include <sys/socket.h> 35 36 #include <net/if.h> 37 #include <net/sff8436.h> 38 #include <net/sff8472.h> 39 40 #include <math.h> 41 #include <err.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <stdbool.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 #include <libutil.h> 51 52 #include <libifconfig.h> 53 #include <libifconfig_sfp.h> 54 55 #include "ifconfig.h" 56 57 void 58 sfp_status(int s, struct ifreq *ifr, int verbose) 59 { 60 struct ifconfig_sfp_info info; 61 struct ifconfig_sfp_info_strings strings; 62 struct ifconfig_sfp_vendor_info vendor_info; 63 struct ifconfig_sfp_status status; 64 ifconfig_handle_t *lifh; 65 size_t channel_count; 66 67 lifh = ifconfig_open(); 68 if (lifh == NULL) 69 return; 70 71 if (ifconfig_sfp_get_sfp_info(lifh, name, &info) == -1) 72 goto close; 73 74 ifconfig_sfp_get_sfp_info_strings(&info, &strings); 75 76 printf("\tplugged: %s %s (%s)\n", 77 ifconfig_sfp_id_display(info.sfp_id), 78 ifconfig_sfp_physical_spec(&info, &strings), 79 strings.sfp_conn); 80 81 if (ifconfig_sfp_get_sfp_vendor_info(lifh, name, &vendor_info) == -1) 82 goto close; 83 84 printf("\tvendor: %s PN: %s SN: %s DATE: %s\n", 85 vendor_info.name, vendor_info.pn, vendor_info.sn, vendor_info.date); 86 87 if (ifconfig_sfp_id_is_qsfp(info.sfp_id)) { 88 if (verbose > 1) 89 printf("\tcompliance level: %s\n", strings.sfp_rev); 90 } else { 91 if (verbose > 5) { 92 printf("Class: %s\n", 93 ifconfig_sfp_physical_spec(&info, &strings)); 94 printf("Length: %s\n", strings.sfp_fc_len); 95 printf("Tech: %s\n", strings.sfp_cab_tech); 96 printf("Media: %s\n", strings.sfp_fc_media); 97 printf("Speed: %s\n", strings.sfp_fc_speed); 98 } 99 } 100 101 if (ifconfig_sfp_get_sfp_status(lifh, name, &status) == 0) { 102 if (ifconfig_sfp_id_is_qsfp(info.sfp_id) && verbose > 1) 103 printf("\tnominal bitrate: %u Mbps\n", status.bitrate); 104 printf("\tmodule temperature: %.2f C voltage: %.2f Volts\n", 105 status.temp, status.voltage); 106 channel_count = ifconfig_sfp_channel_count(&info); 107 for (size_t chan = 0; chan < channel_count; ++chan) { 108 uint16_t rx = status.channel[chan].rx; 109 uint16_t tx = status.channel[chan].tx; 110 printf("\tlane %zu: " 111 "RX power: %.2f mW (%.2f dBm) TX bias: %.2f mA\n", 112 chan + 1, power_mW(rx), power_dBm(rx), bias_mA(tx)); 113 } 114 ifconfig_sfp_free_sfp_status(&status); 115 } 116 117 if (verbose > 2) { 118 struct ifconfig_sfp_dump dump; 119 120 if (ifconfig_sfp_get_sfp_dump(lifh, name, &dump) == -1) 121 goto close; 122 123 if (ifconfig_sfp_id_is_qsfp(info.sfp_id)) { 124 printf("\n\tSFF8436 DUMP (0xA0 128..255 range):\n"); 125 hexdump(dump.data + QSFP_DUMP1_START, QSFP_DUMP1_SIZE, 126 "\t", HD_OMIT_COUNT | HD_OMIT_CHARS); 127 printf("\n\tSFF8436 DUMP (0xA0 0..81 range):\n"); 128 hexdump(dump.data + QSFP_DUMP0_START, QSFP_DUMP0_SIZE, 129 "\t", HD_OMIT_COUNT | HD_OMIT_CHARS); 130 } else { 131 printf("\n\tSFF8472 DUMP (0xA0 0..127 range):\n"); 132 hexdump(dump.data + SFP_DUMP_START, SFP_DUMP_SIZE, 133 "\t", HD_OMIT_COUNT | HD_OMIT_CHARS); 134 } 135 } 136 137 close: 138 ifconfig_close(lifh); 139 } 140