Skip to content

Commit

Permalink
Improve RX functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabbing committed Nov 20, 2024
1 parent e032315 commit 5662947
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/netif.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ external uk_bigarray_of_netbuf : int64 -> bytes_array = "uk_bigarray_of_netbuf"
external uk_netdev_tx : int64 -> int64 -> int -> (unit, string) result
= "uk_netdev_tx"

external uk_netdev_rx : int64 -> Cstruct.buffer -> int -> bool * int * string
external uk_netdev_rx : int64 -> Cstruct.buffer -> int -> (int, string) result
= "uk_netdev_rx"

open Lwt.Infix
Expand Down Expand Up @@ -121,19 +121,21 @@ let rec read t buf =
else
(* TODO what about offset? *)
match uk_netdev_rx t.netif buf.Cstruct.buffer buf.Cstruct.len with
| true, 0, _ -> Lwt.return (Error `Continue)
| true, size, _ ->
| Ok 0 -> Lwt.return (Error `Continue)
| Ok size ->
Mirage_net.Stats.rx t.stats (Int64.of_int size);
let buf = Cstruct.sub buf 0 size in
Lwt.return (Ok buf)
| false, _, err -> Lwt.return (Error `Disconnected)
| Error msg ->
Log.err (fun f -> f "Error receiving: %s" msg);
Lwt.return (Error `Unspecified_error)
in
process () >>= function
| Error `Continue ->
Unikraft_os.Main.UkEngine.wait_for_work_netdev t.id >>= fun () ->
read t buf
| Error `Canceled -> Lwt.return (Error `Canceled)
| Error `Disconnected -> Lwt.return (Error `Disconnected)
| Error `Unspecified_error -> Lwt.return (Error `Disconnected)
| Ok buf -> Lwt.return (Ok buf)

(* Loop and listen for packets permanently *)
Expand Down
18 changes: 6 additions & 12 deletions src/rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,17 @@ static int netdev_rx(struct netif* netif, uint8_t *buf, unsigned *size,
CAMLprim value uk_netdev_rx(value v_netif, value v_buf, value v_size)
{
CAMLparam3(v_netif, v_buf, v_size);
CAMLlocal2(v_result, v_error);
CAMLlocal1(v_result);

struct netif *netif = (struct netif*)Int64_val(v_netif);
uint8_t *buf = (uint8_t *)Caml_ba_data_val(v_buf);
unsigned size = Int_val(v_size);

v_result = caml_alloc_tuple(3);
Store_field(v_result, 0, Val_false);
Store_field(v_result, 1, Val_int(0));
Store_field(v_result, 2, caml_copy_string(""));

const char *err = NULL;

const int rc = netdev_rx(netif, buf, &size, &err);
if (rc < 0) {
uk_pr_err("netdev_rx: failed with %s\n", err);
v_error = caml_copy_string(err);
Store_field(v_result, 2, v_error);
v_result = alloc_result_error(err);
CAMLreturn(v_result);
}

Expand All @@ -125,9 +119,9 @@ CAMLprim value uk_netdev_rx(value v_netif, value v_buf, value v_size)
set_netdev_queue_empty(netif->id);
}

Store_field(v_result, 0, Val_true);
Store_field(v_result, 1, Val_int(size));

uk_pr_debug("uk_netdev_rx: read %d bytes\n", size);

v_result = alloc_result_ok();
Store_field(v_result, 0, Val_int(size));
CAMLreturn(v_result);
}

0 comments on commit 5662947

Please sign in to comment.