Skip to content

Instantly share code, notes, and snippets.

@talex5
Created June 25, 2015 09:31
Show Gist options
  • Save talex5/71ddeb255c2d9339d52b to your computer and use it in GitHub Desktop.
Save talex5/71ddeb255c2d9339d52b to your computer and use it in GitHub Desktop.
Mirage test that sends one byte to TCP endpoint (10.0.0.1/7001), with tracing on
open Mirage
let main = foreign "Unikernel.Main" (stackv4 @-> job)
let stack console =
match get_mode () with
| `Xen -> direct_stackv4_with_default_ipv4 console tap0
| `Unix -> assert false
let tracing = mprof_trace ~size:1000000 ()
let () =
register "bench" ~tracing [
main $ stack default_console;
]
open Lwt
let laptop_ip = Ipaddr.V4.of_string_exn "10.0.0.1"
let () =
let open Tcp in
[Pcb.info; Pcb.debug; State.debug] |> List.iter (fun log ->
Log.enable log;
Log.set_stats log false
)
let () =
Lwt.async_exception_hook := fun ex -> Printf.eprintf "Uncaught exception: %s\n%!" (Printexc.to_string ex)
module Main (S: V1_LWT.STACKV4) = struct
let buffer = Io_page.get 1 |> Io_page.to_cstruct
let start s =
let t = S.tcpv4 s in
match_lwt S.TCPV4.create_connection t (laptop_ip, 7001) with
| `Error _err -> failwith "TCP error 7001"
| `Ok flow ->
let payload = Cstruct.sub buffer 0 1 in
Cstruct.set_char payload 0 '!';
match_lwt S.TCPV4.write flow payload with
| `Error _ | `Eof -> assert false
| `Ok () ->
S.TCPV4.close flow >>= fun () ->
OS.Time.sleep 10.0
end
@talex5
Copy link
Author

talex5 commented Jun 25, 2015

To test, run netcat on 10.0.01 to accept the data (or change laptop_ip):

nc -l -p 7001

Then run the unikernel. With mirage/mirage-tcpip#156, the output is:

Xen Minimal OS!
[...]
Manager: connect
Manager: configuring
Manager: Interface to 10.0.0.2 nm 255.255.255.0 gw [10.0.0.1]

 sg:true gso_tcpv4:true rx_copy:true rx_flip:false smart_poll:false
ARP: sending gratuitous from 10.0.0.2
Manager: configuration done
ARP: transmitting probe -> 10.0.0.1
ARP: retrying 10.0.0.1 (n=1)
ARP: transmitting probe -> 10.0.0.1
ARP: updating 10.0.0.1 -> fe:ff:ff:ff:ff:ff
Tcp.PCB: process-synack: [channels=0 listens=0 connects=1]
Tcp.PCB: new-client-connection: [channels=0 listens=0 connects=0]
Tcp.State: Closed  - Send_syn(466439307) -> Syn_sent(466439307)
Tcp.State: Syn_sent(466439307)  - Recv_synack(466439308) -> Established
Tcp.PCB: TX.close
Tcp.State: Established  - Send_fin(466439309) -> Fin_wait_1(466439309)
Tcp.State: Fin_wait_1(466439309)  - Recv_ack(466439309) -> Fin_wait_1(466439309)
Tcp.State: finwait2timer 10.00
Tcp.State: Fin_wait_1(466439309)  - Recv_ack(466439310) -> Fin_wait_2(0)
Tcp.State: timewait 2.00
Tcp.State: Fin_wait_2(0)  - Recv_fin -> Time_wait
Tcp.State: Time_wait  - Recv_rst -> Time_wait
Tcp.State: Time_wait  - Recv_fin -> Time_wait
Tcp.State: Time_wait  - Recv_rst -> Time_wait
Tcp.State: Time_wait  - Recv_fin -> Time_wait
Tcp.State: Time_wait  - Recv_rst -> Time_wait
Tcp.State: Time_wait  - Recv_fin -> Time_wait
...

@yomimono
Copy link

Perfect, thanks; I was just recreating something from the snippet you put in the issue but I'll grab this and give it a try instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment