Permalink
Browse files

Implement OS.Netif.enumerate

  • Loading branch information...
1 parent 42c41a2 commit 60901d7226820f632770f72cb5b5752ffc025dbf @pgj committed Aug 9, 2012
@@ -3,18 +3,25 @@ LIBNAME= mirage-platform
SRCS!= ls lib/*.ml
MLLIB= lib/oS.cmxa lib/oS.a lib/oS.cmx lib/oS.cmi
-COBJS= runtime/kernel/page_stubs.o runtime/kernel/clock_stubs.o \
- runtime/kernel/kmod.o runtime/ocaml/libocaml.a
+COBJS= runtime/kernel/netif_stubs.o runtime/kernel/page_stubs.o \
+ runtime/kernel/clock_stubs.o runtime/kernel/kmod.o \
+ runtime/ocaml/libocaml.a
PWD!= pwd
-CFLAGS= -O2 -DCAML_NAME_SPACE -DNATIVE_CODE -DSYS_bsd_elf -DTARGET_amd64 -Werror -D_KERNEL \
- -DKLD_MODULE -nostdinc -I${PWD}/runtime/include -I${PWD}/runtime/kernel -I${PWD}/runtime/kernel/@ -finline-limit=8000 --param inline-unit-growth=100 \
- --param large-function-growth=1000 -fno-common -fno-omit-frame-pointer -mcmodel=kernel \
- -mno-red-zone -mno-mmx -msoft-float -fno-asynchronous-unwind-tables -ffreestanding \
- -fstack-protector -std=iso9899:1999 -fstack-protector -Wall -Wredundant-decls -Wnested-externs \
- -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual -Wundef \
- -Wno-pointer-sign -fformat-extensions -Wmissing-include-dirs -fdiagnostics-show-option -mno-sse
+CFLAGS= -O2 -DCAML_NAME_SPACE -DNATIVE_CODE -DSYS_bsd_elf \
+ -DTARGET_amd64 -Werror -D_KERNEL -DKLD_MODULE -nostdinc \
+ -I${PWD}/runtime/include -I${PWD}/runtime/kernel/@ \
+ -I${PWD}/runtime/kernel/@/contrib/altq -I${PWD}/runtime/kernel \
+ -finline-limit=8000 --param inline-unit-growth=100 \
+ --param large-function-growth=1000 -fno-common \
+ -fno-omit-frame-pointer -mcmodel=kernel -mno-red-zone -mno-mmx \
+ -msoft-float -fno-asynchronous-unwind-tables -ffreestanding \
+ -fstack-protector -std=iso9899:1999 -fstack-protector -Wall \
+ -Wredundant-decls -Wnested-externs -Wstrict-prototypes \
+ -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual \
+ -Wundef -Wno-pointer-sign -fformat-extensions \
+ -Wmissing-include-dirs -fdiagnostics-show-option -mno-sse
DEPS= lwt
@@ -36,6 +36,8 @@ type t = {
type id = string
+external get_vifs: unit -> id list = "kern_get_vifs"
+
let plug id =
Console.log (sprintf "Netif.plug %s: not implemented yet" id);
lwt backend_id = return 0 in
@@ -64,8 +66,14 @@ let listen ifc fn =
return ()
let enumerate () =
- Console.log (sprintf "Netif.enumerate: not implemented yet");
- return []
+ let vifs = get_vifs () in
+ let rec read_vif l acc = match l with
+ | [] -> return acc
+ | (x::xs) ->
+ lwt sid = return x in
+ read_vif xs (sid :: acc)
+ in
+ read_vif vifs []
let mac ifc =
Console.log (sprintf "Netif.mac %s: not implemented yet" ifc.backend);
@@ -0,0 +1,74 @@
+/*-
+ * Copyright (c) 2012 Gabor Pali
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/socket.h>
+
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+#include <net/vnet.h>
+
+#include "caml/mlvalues.h"
+#include "caml/memory.h"
+#include "caml/alloc.h"
+
+/* Currently only Ethernet interfaces are returned. */
+CAMLprim value kern_get_vifs(value v_unit);
+
+CAMLprim value
+kern_get_vifs(value v_unit)
+{
+ CAMLparam1(v_unit);
+ CAMLlocal2(result, r);
+ struct ifnet *ifp;
+ struct ifaddr *ifa;
+ struct sockaddr_dl *sdl;
+
+ result = Val_emptylist;
+ IFNET_RLOCK_NOSLEEP();
+ TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
+ IF_ADDR_RLOCK(ifp);
+ TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
+ sdl = (struct sockaddr_dl *) ifa->ifa_addr;
+ if (sdl != NULL && sdl->sdl_family == AF_LINK &&
+ sdl->sdl_type == IFT_ETHER) {
+ /* We have a MAC address, add the interface. */
+ r = caml_alloc(2, 0);
+ Store_field(r, 0,
+ caml_copy_string(ifp->if_xname));
+ Store_field(r, 1, result);
+ result = r;
+ break;
+ }
+ }
+ IF_ADDR_RUNLOCK(ifp);
+ }
+ IFNET_RUNLOCK_NOSLEEP();
+ CAMLreturn(result);
+}
@@ -1,12 +1,19 @@
+
open Lwt
+open Printf
open OS.Clock
open OS.Console
open OS.Time
open OS.Main
+open OS.Netif
let rec fib n =
if n < 2 then 1 else fib (n - 1) + fib (n - 2)
+let rec print_vifs l = match_lwt l with
+ | [] -> return ()
+ | (x::xs) -> log_s x >> print_vifs (return xs)
+
let f () =
log_s "Sorry, no in-kernel Mirage today!" >>
log_s "And there is a second message..." >>
@@ -17,15 +24,14 @@ let f () =
let t2 = time () in
log_s "... and now wake up!" >>
let passed = (t2 - t1) / 1000 in
- let msg = Printf.sprintf "Time passed: %d ms.\n" passed in
- log_s msg >>
+ log_s (sprintf "Time passed: %d ms.\n" passed) >>
log_s "Let's do some Fibonacci!" >>
let n = 42 in
- let msg = Printf.sprintf "fib %d = " n in
- log_s msg >>
- let msg = Printf.sprintf "%d" (fib n) in
- log_s msg >>
- return ()
+ log_s (sprintf "fib %d = " n) >>
+ log_s (sprintf "%d" (fib n)) >>
+ log_s "Available network interfaces: " >>
+ let ifs = enumerate () in
+ print_vifs ifs
let day_of n = match n with
| 0 -> "Sun"

0 comments on commit 60901d7

Please sign in to comment.