[BACK]Return to mroute.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / netstat

Annotation of src/usr.bin/netstat/mroute.c, Revision 1.13

1.13    ! jaredy      1: /*     $OpenBSD: mroute.c,v 1.12 2005/01/14 15:00:44 mcbride Exp $     */
1.2       deraadt     2: /*     $NetBSD: mroute.c,v 1.10 1996/05/11 13:51:27 mycroft Exp $      */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1989 Stephen Deering
                      6:  * Copyright (c) 1992, 1993
                      7:  *     The Regents of the University of California.  All rights reserved.
                      8:  *
                      9:  * This code is derived from software contributed to Berkeley by
                     10:  * Stephen Deering of Stanford University.
                     11:  *
                     12:  * Redistribution and use in source and binary forms, with or without
                     13:  * modification, are permitted provided that the following conditions
                     14:  * are met:
                     15:  * 1. Redistributions of source code must retain the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer.
                     17:  * 2. Redistributions in binary form must reproduce the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer in the
                     19:  *    documentation and/or other materials provided with the distribution.
1.10      millert    20:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  *
                     36:  *     from: @(#)mroute.c      8.1 (Berkeley) 6/6/93
                     37:  */
                     38:
                     39: /*
1.12      mcbride    40:  * Print multicast routing structures and statistics.
1.1       deraadt    41:  *
                     42:  * MROUTING 1.0
                     43:  */
                     44:
                     45: #include <sys/param.h>
                     46: #include <sys/socket.h>
                     47: #include <sys/socketvar.h>
                     48: #include <sys/protosw.h>
                     49:
                     50: #include <net/if.h>
                     51: #include <net/route.h>
                     52: #include <netinet/in.h>
                     53: #include <netinet/igmp.h>
                     54: #define _KERNEL
                     55: #include <netinet/ip_mroute.h>
                     56: #undef _KERNEL
                     57:
1.5       millert    58: #include <limits.h>
1.1       deraadt    59: #include <stdio.h>
                     60: #include <stdlib.h>
                     61: #include "netstat.h"
                     62:
1.12      mcbride    63: static void print_bw_meter(struct bw_meter *bw_meter, int *banner_printed);
                     64:
1.11      deraadt    65: static char *
1.9       deraadt    66: pktscale(u_long n)
1.1       deraadt    67: {
                     68:        static char buf[8];
                     69:        char t;
                     70:
                     71:        if (n < 1024)
                     72:                t = ' ';
                     73:        else if (n < 1024 * 1024) {
                     74:                t = 'k';
                     75:                n /= 1024;
                     76:        } else {
                     77:                t = 'm';
                     78:                n /= 1048576;
                     79:        }
                     80:
1.6       deraadt    81:        snprintf(buf, sizeof buf, "%lu%c", n, t);
1.1       deraadt    82:        return (buf);
                     83: }
                     84:
                     85: void
1.9       deraadt    86: mroutepr(u_long mrpaddr, u_long mfchashtbladdr, u_long mfchashaddr, u_long vifaddr)
1.1       deraadt    87: {
                     88:        u_int mrtproto;
                     89:        LIST_HEAD(, mfc) *mfchashtbl;
                     90:        u_long mfchash;
                     91:        struct vif viftable[MAXVIFS];
                     92:        struct mfc *mfcp, mfc;
1.7       mpech      93:        struct vif *v;
                     94:        vifi_t vifi;
1.1       deraadt    95:        int i;
                     96:        int banner_printed;
                     97:        int saved_nflag;
                     98:        int numvifs;
                     99:        int nmfc;               /* No. of cache entries */
                    100:
                    101:        if (mrpaddr == 0) {
                    102:                printf("ip_mrtproto: symbol not in namelist\n");
                    103:                return;
                    104:        }
                    105:
1.13    ! jaredy    106:        kread(mrpaddr, &mrtproto, sizeof(mrtproto));
1.1       deraadt   107:        switch (mrtproto) {
                    108:        case 0:
                    109:                printf("no multicast routing compiled into this system\n");
                    110:                return;
                    111:
                    112:        case IGMP_DVMRP:
                    113:                break;
                    114:
                    115:        default:
                    116:                printf("multicast routing protocol %u, unknown\n", mrtproto);
                    117:                return;
                    118:        }
                    119:
                    120:        if (mfchashtbladdr == 0) {
                    121:                printf("mfchashtbl: symbol not in namelist\n");
                    122:                return;
                    123:        }
                    124:        if (mfchashaddr == 0) {
                    125:                printf("mfchash: symbol not in namelist\n");
                    126:                return;
                    127:        }
                    128:        if (vifaddr == 0) {
                    129:                printf("viftable: symbol not in namelist\n");
                    130:                return;
                    131:        }
                    132:
                    133:        saved_nflag = nflag;
                    134:        nflag = 1;
                    135:
1.13    ! jaredy    136:        kread(vifaddr, &viftable, sizeof(viftable));
1.1       deraadt   137:        banner_printed = 0;
                    138:        numvifs = 0;
                    139:
                    140:        for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
                    141:                if (v->v_lcl_addr.s_addr == 0)
                    142:                        continue;
                    143:                numvifs = vifi;
                    144:
                    145:                if (!banner_printed) {
                    146:                        printf("\nVirtual Interface Table\n %s%s",
                    147:                            "Vif  Thresh  Limit  Local-Address    ",
                    148:                            "Remote-Address   Pkt_in  Pkt_out\n");
                    149:                        banner_printed = 1;
                    150:                }
                    151:
                    152:                printf(" %3u     %3u  %5u  %-15.15s",
                    153:                    vifi, v->v_threshold, v->v_rate_limit,
                    154:                    routename(v->v_lcl_addr.s_addr));
1.4       millert   155:                printf("  %-15.15s  %6lu  %7lu\n", (v->v_flags & VIFF_TUNNEL) ?
1.1       deraadt   156:                    routename(v->v_rmt_addr.s_addr) : "",
                    157:                    v->v_pkt_in, v->v_pkt_out);
                    158:        }
                    159:        if (!banner_printed)
                    160:                printf("\nVirtual Interface Table is empty\n");
                    161:
1.13    ! jaredy    162:        kread(mfchashtbladdr, &mfchashtbl, sizeof(mfchashtbl));
        !           163:        kread(mfchashaddr, &mfchash, sizeof(mfchash));
1.1       deraadt   164:        banner_printed = 0;
                    165:        nmfc = 0;
                    166:
1.2       deraadt   167:        if (mfchashtbl != 0)
                    168:                for (i = 0; i <= mfchash; ++i) {
1.13    ! jaredy    169:                        kread((u_long)&mfchashtbl[i], &mfcp, sizeof(mfcp));
1.2       deraadt   170:
                    171:                        for (; mfcp != 0; mfcp = mfc.mfc_hash.le_next) {
                    172:                                if (!banner_printed) {
                    173:                                        printf("\nMulticast Forwarding Cache\n %s%s",
                    174:                                            "Hash  Origin           Mcastgroup       ",
                    175:                                            "Traffic  In-Vif  Out-Vifs/Forw-ttl\n");
                    176:                                        banner_printed = 1;
                    177:                                }
                    178:
1.13    ! jaredy    179:                                kread((u_long)mfcp, &mfc, sizeof(mfc));
1.2       deraadt   180:                                printf("  %3u  %-15.15s",
                    181:                                    i, routename(mfc.mfc_origin.s_addr));
                    182:                                printf("  %-15.15s  %7s     %3u ",
                    183:                                    routename(mfc.mfc_mcastgrp.s_addr),
                    184:                                    pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent);
                    185:                                for (vifi = 0; vifi <= numvifs; ++vifi)
                    186:                                        if (mfc.mfc_ttls[vifi])
                    187:                                                printf(" %u/%u", vifi,
                    188:                                                    mfc.mfc_ttls[vifi]);
1.1       deraadt   189:
1.2       deraadt   190:                                printf("\n");
1.12      mcbride   191:
                    192:                                /* Print the bw meter information */
                    193:                                {
                    194:                                        struct bw_meter bw_meter, *bwm;
                    195:                                        int banner_printed2 = 0;
                    196:
                    197:                                        bwm = mfc.mfc_bw_meter;
                    198:                                        while (bwm) {
                    199:                                                kread((u_long)bwm,
1.13    ! jaredy    200:                                                      &bw_meter,
1.12      mcbride   201:                                                      sizeof bw_meter);
                    202:                                                print_bw_meter(&bw_meter,
                    203:                                                               &banner_printed2);
                    204:                                                bwm = bw_meter.bm_mfc_next;
                    205:                                        }
                    206: #if 0  /* Don't ever print it? */
                    207:                                        if (! banner_printed2)
                    208:                                                printf("\n  No Bandwidth Meters\n");
                    209: #endif
                    210:                                }
                    211:
1.2       deraadt   212:                                nmfc++;
1.1       deraadt   213:                        }
                    214:                }
                    215:        if (!banner_printed)
                    216:                printf("\nMulticast Forwarding Cache is empty\n");
                    217:        else
                    218:                printf("\nTotal no. of entries in cache: %d\n", nmfc);
                    219:
                    220:        printf("\n");
                    221:        nflag = saved_nflag;
                    222: }
                    223:
1.12      mcbride   224: static void
                    225: print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
                    226: {
                    227:        char s0[256], s1[256], s2[256], s3[256];
                    228:        struct timeval now, end, delta;
                    229:
                    230:        gettimeofday(&now, NULL);
                    231:
                    232:        if (! *banner_printed) {
                    233:                printf(" Bandwidth Meters\n");
                    234:                printf("  %-30s", "Measured(Start|Packets|Bytes)");
                    235:                printf(" %s", "Type");
                    236:                printf("  %-30s", "Thresh(Interval|Packets|Bytes)");
                    237:                printf(" Remain");
                    238:                printf("\n");
                    239:                *banner_printed = 1;
                    240:        }
                    241:
                    242:        /* The measured values */
                    243:        if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
                    244:                snprintf(s1, sizeof s1, "%llu",
                    245:                         bw_meter->bm_measured.b_packets);
                    246:        else
                    247:                snprintf(s1, sizeof s1, "?");
                    248:        if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
                    249:                snprintf(s2, sizeof s2, "%llu", bw_meter->bm_measured.b_bytes);
                    250:        else
                    251:                snprintf(s2, sizeof s2, "?");
                    252:        snprintf(s0, sizeof s0, "%lu.%lu|%s|%s",
                    253:                 bw_meter->bm_start_time.tv_sec,
                    254:                 bw_meter->bm_start_time.tv_usec,
                    255:                 s1, s2);
                    256:        printf("  %-30s", s0);
                    257:
                    258:        /* The type of entry */
                    259:        snprintf(s0, sizeof s0, "%s", "?");
                    260:        if (bw_meter->bm_flags & BW_METER_GEQ)
                    261:                snprintf(s0, sizeof s0, "%s", ">=");
                    262:        else if (bw_meter->bm_flags & BW_METER_LEQ)
                    263:                snprintf(s0, sizeof s0, "%s", "<=");
                    264:        printf("  %-3s", s0);
                    265:
                    266:        /* The threshold values */
                    267:        if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
                    268:                snprintf(s1, sizeof s1, "%llu",
                    269:                         bw_meter->bm_threshold.b_packets);
                    270:        else
                    271:                snprintf(s1, sizeof s1, "?");
                    272:        if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
                    273:                snprintf(s2, sizeof s2, "%llu",
                    274:                         bw_meter->bm_threshold.b_bytes);
                    275:        else
                    276:                snprintf(s2, sizeof s2, "?");
                    277:        snprintf(s0, sizeof s0, "%lu.%lu|%s|%s",
                    278:                 bw_meter->bm_threshold.b_time.tv_sec,
                    279:                 bw_meter->bm_threshold.b_time.tv_usec,
                    280:                 s1, s2);
                    281:        printf("  %-30s", s0);
                    282:
                    283:        /* Remaining time */
                    284:        timeradd(&bw_meter->bm_start_time,
                    285:                 &bw_meter->bm_threshold.b_time, &end);
                    286:        if (timercmp(&now, &end, <=)) {
                    287:                timersub(&end, &now, &delta);
                    288:                snprintf(s3, sizeof s3, "%lu.%lu",
                    289:                         delta.tv_sec, delta.tv_usec);
                    290:        } else {
                    291:                /* Negative time */
                    292:                timersub(&now, &end, &delta);
                    293:                snprintf(s3, sizeof s3, "-%lu.%lu",
                    294:                         delta.tv_sec, delta.tv_usec);
                    295:        }
                    296:        printf(" %s", s3);
                    297:
                    298:        printf("\n");
                    299: }
1.1       deraadt   300:
                    301: void
1.9       deraadt   302: mrt_stats(u_long mrpaddr, u_long mstaddr)
1.1       deraadt   303: {
                    304:        u_int mrtproto;
                    305:        struct mrtstat mrtstat;
                    306:
                    307:        if (mrpaddr == 0) {
                    308:                printf("ip_mrtproto: symbol not in namelist\n");
                    309:                return;
                    310:        }
                    311:
1.13    ! jaredy    312:        kread(mrpaddr, &mrtproto, sizeof(mrtproto));
1.1       deraadt   313:        switch (mrtproto) {
                    314:        case 0:
                    315:                printf("no multicast routing compiled into this system\n");
                    316:                return;
                    317:
                    318:        case IGMP_DVMRP:
                    319:                break;
                    320:
                    321:        default:
                    322:                printf("multicast routing protocol %u, unknown\n", mrtproto);
                    323:                return;
                    324:        }
                    325:
                    326:        if (mstaddr == 0) {
                    327:                printf("mrtstat: symbol not in namelist\n");
                    328:                return;
                    329:        }
                    330:
1.13    ! jaredy    331:        kread(mstaddr, &mrtstat, sizeof(mrtstat));
1.1       deraadt   332:        printf("multicast routing:\n");
1.8       itojun    333:        printf("\t%lu datagram%s with no route for origin\n",
1.1       deraadt   334:            mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
1.8       itojun    335:        printf("\t%lu upcall%s made to mrouted\n",
1.1       deraadt   336:            mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
1.8       itojun    337:        printf("\t%lu datagram%s with malformed tunnel options\n",
1.1       deraadt   338:            mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
1.8       itojun    339:        printf("\t%lu datagram%s with no room for tunnel options\n",
1.1       deraadt   340:            mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
1.8       itojun    341:        printf("\t%lu datagram%s arrived on wrong interface\n",
1.1       deraadt   342:            mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
1.8       itojun    343:        printf("\t%lu datagram%s dropped due to upcall Q overflow\n",
1.1       deraadt   344:            mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
1.8       itojun    345:        printf("\t%lu datagram%s dropped due to upcall socket overflow\n",
1.1       deraadt   346:            mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
1.8       itojun    347:        printf("\t%lu datagram%s cleaned up by the cache\n",
1.1       deraadt   348:            mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
1.8       itojun    349:        printf("\t%lu datagram%s dropped selectively by ratelimiter\n",
1.1       deraadt   350:            mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
1.8       itojun    351:        printf("\t%lu datagram%s dropped - bucket Q overflow\n",
1.1       deraadt   352:            mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
1.8       itojun    353:        printf("\t%lu datagram%s dropped - larger than bkt size\n",
1.1       deraadt   354:            mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
                    355: }