Remove these kernel info files without data.
[virt-mem.git] / ifconfig / virt_ifconfig.ml
1 (* Memory info for virtual domains.
2    (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *)
19
20 open Printf
21
22 open Virt_mem_gettext.Gettext
23 open Virt_mem_utils
24 open Virt_mem_types
25
26 open Kernel
27
28 let run debug { addrmap = addrmap } =
29   (* Get all net_devices.  When people really start to use network
30    * namespaces, we'll need to divide these by namespace (ie. using
31    * struct net). XXX
32    *)
33   let net_devices = AddrMap.fold (
34     fun _ v devs ->
35       match v with
36       | _, Some (_, _, Net_device dev) -> dev :: devs
37       | _ -> devs
38   ) addrmap [] in
39
40   let net_devices = List.map (
41     fun ({ net_device_name = name } as dev) ->
42       { dev with net_device_name = truncate_c_string name }
43   ) net_devices in
44 (*
45   If you want to sort by name ...
46   let cmp { net_device_name = n1 } { net_device_name = n2 } = compare n1 n2 in
47   let net_devices = List.sort cmp net_devices in
48 *)
49
50   (* Get the IPv4 and IPv6 addresses. *)
51   let net_devices = List.map (
52     fun ({ net_device_ip_ptr = ip4 (* pointer to in_device *);
53            net_device_ip6_ptr = ip6 (* pointer to inet6_dev *) } as dev) ->
54       let ip4 =
55         match AddrMap.find ip4 addrmap with
56         | _, Some (_, _, In_device d) ->
57             d.in_device_ifa_list (* pointer to in_ifaddr *)
58         | _ -> assert false in
59       let ip4addrs =
60         let rec loop addr =
61           if addr <> 0L then (
62             let ifaddr =
63               match AddrMap.find addr addrmap with
64               | _, Some (_, _, In_ifaddr d) -> d
65               | _ -> assert false in
66             (ifaddr.in_ifaddr_ifa_address,
67              ifaddr.in_ifaddr_ifa_broadcast,
68              ifaddr.in_ifaddr_ifa_local,
69              ifaddr.in_ifaddr_ifa_mask)
70             :: loop ifaddr.in_ifaddr_ifa_next
71           ) else []
72         in
73         loop ip4 in
74       let ip6 =
75         match AddrMap.find ip6 addrmap with
76         | _, Some (_, _, Inet6_dev d) ->
77             d.inet6_dev_addr_list (* pointer to inet6_ifaddr *)
78         | _ -> assert false in
79       let ip6addrs =
80         let rec loop addr =
81           if addr <> 0L then (
82             let ifaddr =
83               match AddrMap.find addr addrmap with
84               | _, Some (_, _, Inet6_ifaddr d) -> d
85               | _ -> assert false in
86             ifaddr.inet6_ifaddr_prefix_len
87             :: loop ifaddr.inet6_ifaddr_lst_next
88           ) else []
89         in
90         loop ip6 in
91
92       dev, ip4addrs, ip6addrs
93   ) net_devices in
94
95   (* Print out the interfaces and addresses. *)
96   printf
97     "NAME MTU   ADDR             BROADCAST        LOCAL            NETMASK\n";
98
99   List.iter (
100     fun (dev, ip4addrs, ip6addrs) ->
101       printf "%-5s%-6Ld" dev.net_device_name dev.net_device_mtu;
102
103       let rec loop = function
104         | [] -> ()
105         | [addr] -> print addr
106         | addr :: addrs -> print addr; printf "\n           "; loop addrs
107
108       and print (addr, broadcast, local, mask) =
109         printf "%-16s %-16s %-16s %-16s"
110           (string_of_ipv4 addr)
111           (string_of_ipv4 broadcast)
112           (string_of_ipv4 local)
113           (string_of_ipv4 mask)
114
115       and string_of_ipv4 addr =
116         (* Always stored big-endian, even in a little endian kernel: *)
117         let d = Int64.shift_right_logical (Int64.logand addr 0xff000000L) 24
118         and c = Int64.shift_right_logical (Int64.logand addr 0x00ff0000L) 16
119         and b = Int64.shift_right_logical (Int64.logand addr 0x0000ff00L) 8
120         and a =                            Int64.logand addr 0x000000ffL in
121         sprintf "%Ld.%Ld.%Ld.%Ld" a b c d
122       in
123       loop ip4addrs;
124       printf "\n";
125
126       (* XXX ip6addrs too *)
127   ) net_devices
128
129 let summary = s_"list network interfaces in virtual machine"
130 let description = s_"\
131 virt-ifconfig prints a network interfaces for virtual machines.
132 "
133
134 let () =
135   Virt_mem.register "ifconfig" summary description ~needs_net_devices:true ~run