1 /* Interface name and statistics get function using proc file system
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #ifdef BRCM_LIST_SUPPORT
30 #include "zebra/ioctl.h"
31 #include "zebra/connected.h"
32 #include "zebra/interface.h"
34 /* Proc filesystem one line buffer. */
35 #define PROCBUFSIZ 1024
37 /* Path to device proc file system. */
38 #ifndef _PATH_PROC_NET_DEV
39 #define _PATH_PROC_NET_DEV "/proc/net/dev"
40 #endif /* _PATH_PROC_NET_DEV */
42 /* Return statistics data pointer. */
44 interface_name_cut (char *buf, char **name)
48 /* Skip white space. Line will include header spaces. */
53 /* Cut interface name. */
54 stat = strrchr (buf, ':');
60 /* Fetch each statistics field. */
62 ifstat_dev_fields (int version, char *buf, struct interface *ifp)
68 "%ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld",
70 &ifp->stats.rx_packets,
71 &ifp->stats.rx_errors,
72 &ifp->stats.rx_dropped,
73 &ifp->stats.rx_fifo_errors,
74 &ifp->stats.rx_frame_errors,
75 &ifp->stats.rx_compressed,
76 &ifp->stats.rx_multicast,
79 &ifp->stats.tx_packets,
80 &ifp->stats.tx_errors,
81 &ifp->stats.tx_dropped,
82 &ifp->stats.tx_fifo_errors,
83 &ifp->stats.collisions,
84 &ifp->stats.tx_carrier_errors,
85 &ifp->stats.tx_compressed);
88 sscanf(buf, "%ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld",
90 &ifp->stats.rx_packets,
91 &ifp->stats.rx_errors,
92 &ifp->stats.rx_dropped,
93 &ifp->stats.rx_fifo_errors,
94 &ifp->stats.rx_frame_errors,
97 &ifp->stats.tx_packets,
98 &ifp->stats.tx_errors,
99 &ifp->stats.tx_dropped,
100 &ifp->stats.tx_fifo_errors,
101 &ifp->stats.collisions,
102 &ifp->stats.tx_carrier_errors);
103 ifp->stats.rx_multicast = 0;
106 sscanf(buf, "%ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld",
107 &ifp->stats.rx_packets,
108 &ifp->stats.rx_errors,
109 &ifp->stats.rx_dropped,
110 &ifp->stats.rx_fifo_errors,
111 &ifp->stats.rx_frame_errors,
113 &ifp->stats.tx_packets,
114 &ifp->stats.tx_errors,
115 &ifp->stats.tx_dropped,
116 &ifp->stats.tx_fifo_errors,
117 &ifp->stats.collisions,
118 &ifp->stats.tx_carrier_errors);
119 ifp->stats.rx_bytes = 0;
120 ifp->stats.tx_bytes = 0;
121 ifp->stats.rx_multicast = 0;
127 /* Update interface's statistics. */
129 ifstat_update_proc ()
132 char buf[PROCBUFSIZ];
134 struct interface *ifp;
138 /* Open /proc/net/dev. */
139 fp = fopen (_PATH_PROC_NET_DEV, "r");
142 zlog_warn ("Can't open proc file %s: %s",
143 _PATH_PROC_NET_DEV, strerror (errno));
147 /* Drop header lines. */
148 fgets (buf, PROCBUFSIZ, fp);
149 fgets (buf, PROCBUFSIZ, fp);
151 /* To detect proc format veresion, parse second line. */
152 if (strstr (buf, "compressed"))
154 else if (strstr (buf, "bytes"))
159 /* Update each interface's statistics. */
160 while (fgets (buf, PROCBUFSIZ, fp) != NULL)
162 stat = interface_name_cut (buf, &name);
163 ifp = if_get_by_name (name);
164 ifstat_dev_fields (version, stat, ifp);
170 /* Interface structure allocation by proc filesystem. */
172 interface_list_proc ()
175 char buf[PROCBUFSIZ];
176 struct interface *ifp;
179 /* Open /proc/net/dev. */
180 fp = fopen (_PATH_PROC_NET_DEV, "r");
183 zlog_warn ("Can't open proc file %s: %s",
184 _PATH_PROC_NET_DEV, strerror (errno));
188 /* Drop header lines. */
189 fgets (buf, PROCBUFSIZ, fp);
190 fgets (buf, PROCBUFSIZ, fp);
192 /* Only allocate interface structure. Other jobs will be done in
194 while (fgets (buf, PROCBUFSIZ, fp) != NULL)
196 interface_name_cut (buf, &name);
197 ifp = if_get_by_name (name);
203 #if defined(HAVE_IPV6) && defined(HAVE_PROC_NET_IF_INET6)
205 #ifndef _PATH_PROC_NET_IF_INET6
206 #define _PATH_PROC_NET_IF_INET6 "/proc/net/if_inet6"
207 #endif /* _PATH_PROC_NET_IF_INET6 */
213 char buf[PROCBUFSIZ];
217 int ifindex, plen, scope, status;
218 struct interface *ifp;
219 struct prefix_ipv6 p;
221 /* Open proc file system. */
222 fp = fopen (_PATH_PROC_NET_IF_INET6, "r");
225 zlog_warn ("Can't open proc file %s: %s",
226 _PATH_PROC_NET_IF_INET6, strerror (errno));
230 /* Get interface's IPv6 address. */
231 while (fgets (buf, PROCBUFSIZ, fp) != NULL)
233 n = sscanf (buf, "%32s %02x %02x %02x %02x %20s",
234 addr, &ifindex, &plen, &scope, &status, ifname);
238 ifp = if_get_by_name (ifname);
240 /* Fetch interface's IPv6 address. */
241 str2in6_addr (addr, &p.prefix);
244 connected_add_ipv6 (ifp, &p.prefix, p.prefixlen, NULL);
248 #endif /* HAVE_IPV6 && HAVE_PROC_NET_IF_INET6 */
250 #endif /* BRCM_LIST_SUPPORT */