1 (* Memory info for virtual domains.
2 (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
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.
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.
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.
22 open Virt_mem_gettext.Gettext
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
33 let net_devices = AddrMap.fold (
36 | _, Some (_, _, Net_device dev) -> dev :: devs
40 let net_devices = List.map (
41 fun ({ net_device_name = name } as dev) ->
42 { dev with net_device_name = truncate_c_string name }
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
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) ->
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
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
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
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
92 dev, ip4addrs, ip6addrs
95 (* Print out the interfaces and addresses. *)
97 "NAME MTU ADDR BROADCAST LOCAL NETMASK\n";
100 fun (dev, ip4addrs, ip6addrs) ->
101 printf "%-5s%-6Ld" dev.net_device_name dev.net_device_mtu;
103 let rec loop = function
105 | [addr] -> print addr
106 | addr :: addrs -> print addr; printf "\n "; loop addrs
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)
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
126 (* XXX ip6addrs too *)
129 let summary = s_"list network interfaces in virtual machine"
130 let description = s_"\
131 virt-ifconfig prints a network interfaces for virtual machines.
135 Virt_mem.register "ifconfig" summary description ~needs_net_devices:true ~run