2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 # include <linux/unistd.h>
42 # include <linux/signal.h>
45 #else /* ! __KERNEL__ */
51 #define _GNU_SOURCE /* to get REG_* in ucontext.h */
60 #include <sys/socket.h>
64 #include <vppinfra/time.h>
67 #include <linux/types.h>
68 #include <linux/netlink.h>
72 #endif /* ! __KERNEL__ */
76 # include <linux/socket.h>
77 # include <linux/in.h>
78 # include <linux/ip.h>
79 # include <linux/tcp.h>
80 # include <linux/udp.h>
81 # include <linux/icmp.h>
82 # include <linux/if_ether.h>
83 # include <linux/if_arp.h>
85 # include <net/if.h> /* struct ifnet may live here */
86 # include <netinet/in.h>
87 # include <netinet/ip.h>
88 # include <netinet/tcp.h>
89 # include <netinet/udp.h>
90 # include <netinet/ip_icmp.h>
91 # include <netinet/if_ether.h>
92 #endif /* __KERNEL__ */
94 #include <vppinfra/bitops.h> /* foreach_set_bit */
95 #include <vppinfra/format.h>
96 #include <vppinfra/error.h>
98 /* Format unix network address family (e.g. AF_INET). */
99 u8 * format_address_family (u8 * s, va_list * va)
101 uword family = va_arg (*va, uword);
102 u8 * t = (u8 *) "UNKNOWN";
105 #define _(x) case PF_##x: t = (u8 *) #x; break
107 _ (UNIX); /* Unix domain sockets */
108 _ (INET); /* Internet IP Protocol */
110 _ (AX25); /* Amateur Radio AX.25 */
113 _ (IPX); /* Novell IPX */
116 _ (APPLETALK); /* AppleTalk DDP */
119 _ (NETROM); /* Amateur Radio NET/ROM */
122 _ (BRIDGE); /* Multiprotocol bridge */
125 _ (ATMPVC); /* ATM PVCs */
128 _ (X25); /* Reserved for X.25 project */
131 _ (INET6); /* IP version 6 */
134 _ (ROSE); /* Amateur Radio X.25 PLP */
137 _ (DECnet); /* Reserved for DECnet project */
140 _ (NETBEUI); /* Reserved for 802.2LLC project*/
143 _ (SECURITY); /* Security callback pseudo AF */
146 _ (KEY); /* PF_KEY key management API */
152 _ (PACKET); /* Packet family */
158 _ (ECONET); /* Acorn Econet */
161 _ (ATMSVC); /* ATM SVCs */
164 _ (SNA); /* Linux SNA Project */
167 _ (IRDA); /* IRDA sockets */
171 vec_add (s, t, strlen ((char *) t));
175 u8 * format_network_protocol (u8 * s, va_list * args)
177 uword family = va_arg (*args, uword);
178 uword protocol = va_arg (*args, uword);
181 struct protoent * p = getprotobynumber (protocol);
183 ASSERT (family == AF_INET);
185 return format (s, "%s", p->p_name);
187 return format (s, "%d", protocol);
189 return format (s, "%d/%d", family, protocol);
193 u8 * format_network_port (u8 * s, va_list * args)
195 uword proto = va_arg (*args, uword);
196 uword port = va_arg (*args, uword);
199 struct servent * p = getservbyport (port, proto == IPPROTO_UDP ? "udp" : "tcp");
202 return format (s, "%s", p->s_name);
204 return format (s, "%d", port);
206 return format (s, "%s/%d", proto == IPPROTO_UDP ? "udp" : "tcp", port);
210 /* Format generic network address: takes two arguments family and address.
211 Assumes network byte order. */
212 u8 * format_network_address (u8 * s, va_list * args)
214 uword family = va_arg (*args, uword);
215 u8 * addr = va_arg (*args, u8 *);
220 s = format (s, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
224 /* We use AF_UNSPEC for ethernet addresses. */
225 s = format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
226 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
230 clib_error ("unsupported address family %d", family);
236 u8 * format_sockaddr (u8 * s, va_list * args)
238 void * v = va_arg (*args, void *);
239 struct sockaddr * sa = v;
240 static u32 local_counter;
242 switch (sa->sa_family)
246 struct sockaddr_in * i = v;
247 s = format (s, "%U:%U",
248 format_network_address, AF_INET, &i->sin_addr.s_addr,
249 format_network_port, IPPROTO_TCP, ntohs (i->sin_port));
256 * There isn't anything useful to print.
257 * The unix cli world uses the output to make a node name,
258 * so we need to return a unique name.
260 s = format (s, "local:%u", local_counter++);
268 struct sockaddr_nl * n = v;
269 s = format (s, "KERNEL-NETLINK");
271 s = format (s, " (groups 0x%x)", n->nl_groups);
278 s = format (s, "sockaddr family %d", sa->sa_family);
288 u8 * format_timeval (u8 * s, va_list * args)
290 char * fmt = va_arg (*args, char *);
291 struct timeval * tv = va_arg (*args, struct timeval *);
297 fmt = "y/m/d H:M:S:F";
301 static struct timeval now;
302 gettimeofday (&now, 0);
306 msec = flt_round_nearest (1e-3 * tv->tv_usec);
308 { msec = 0; tv->tv_sec++; }
311 time_t t = tv->tv_sec;
315 for (f = fmt; *f; f++)
318 char * what_fmt = "%d";
327 what = 1900 + tm->tm_year;
331 what = tm->tm_mon + 1;
356 s = format (s, what_fmt, what);
363 u8 * format_time_float (u8 * s, va_list * args)
365 u8 * fmt = va_arg (*args, u8 *);
366 f64 t = va_arg (*args, f64);
369 t = unix_time_now ();
371 tv.tv_usec = 1e6*(t - tv.tv_sec);
372 return format (s, "%U", format_timeval, fmt, &tv);
375 u8 * format_signal (u8 * s, va_list * args)
377 uword signum = va_arg (*args, uword);
381 #define _(x) case x: t = #x; break;
419 return format (s, "unknown %d", signum);
422 vec_add (s, t, strlen (t));
426 u8 * format_ucontext_pc (u8 * s, va_list * args)
428 ucontext_t * uc __attribute__((unused));
429 unsigned long * regs = 0;
432 uc = va_arg (*args, ucontext_t *);
434 #if defined (powerpc)
435 regs = &uc->uc_mcontext.uc_regs->gregs[0];
436 #elif defined (powerpc64)
437 regs = &uc->uc_mcontext.uc_regs->gp_regs[0];
438 #elif defined (i386) || defined (__x86_64__)
439 regs = (void *) &uc->uc_mcontext.gregs[0];
442 #if defined (powerpc) || defined (powerpc64)
446 #elif defined (__x86_64__)
454 return format (s, "unsupported");
456 return format (s, "%p", regs[reg_no]);
460 unformat_unix_gid (unformat_input_t * input, va_list * args)
462 gid_t *gid = va_arg (*args, gid_t *);
463 struct group *grp = 0;
467 if (unformat (input, "%d", &r))
471 else if (unformat (input, "%s", &s))
473 grp = getgrnam ((char *) s);
484 #endif /* __KERNEL__ */