Stream: general

Topic: netdev not set error


view this post on Zulip celine santosh (Aug 07 2024 at 10:24):

Hi

I have a go app which makes a tcp connection to an ip.
here is the go program

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
)


func main() {
    fmt.Println("I am from Go app")

    // Make a simple HTTP GET request
    resp, err := http.Get("http://example.com")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(1)
    }

    // Read the response body
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        os.Exit(1)
    }
    resp.Body.Close()

    fmt.Printf("Response status: %s\n", resp.Status)
    fmt.Printf("Response body: %s\n", body)
}

and I have compiled it to wasip2 target using tiny Go dev branch.

then I have wasmtime main.rs rust file which loads and instantiates and calls the main function go go app

use wasmtime::{Engine, Result, Store, Config};

use wasmtime::component::{ResourceTable, Linker, Component};

use wasmtime_wasi::{WasiCtx, WasiView, WasiCtxBuilder};

use std::sync::{Arc, Mutex};

use anyhow::{Context};

struct MyState {

ctx: WasiCtx,

table: ResourceTable,

}

impl WasiView for MyState {

fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }

fn table(&mut self) -> &mut ResourceTable { &mut self.table }

}

fn main() -> Result<()> {

let mut config = Config::new();

config.wasm_component_model(true);

let engine = Engine::new(&config)?;

let mut linker = Linker::<MyState>::new(&engine);

wasmtime_wasi::add_to_linker_sync(&mut linker)?;

let wasi_ctx = WasiCtxBuilder::new().inherit_stdio().inherit_network().build();

let state = MyState {

ctx: wasi_ctx,

table: ResourceTable::new(),

};

let mut store = Store::new(&engine, state);



let module_go_app= Component::from_file(&engine, "/Users/celinesantosh/Desktop/sem_5/go_program/go_app.wasm")

.context("failed to read go_app.wasm")?;

let instance = linker.instantiate(&mut store, &module_go_app)

.context("failed to instantiate go_app")?;

let command = wasmtime_wasi::bindings::sync::Command::instantiate(

&mut store,

&module_go_app,

&linker,

)?;

let result = command

.wasi_cli_run()

.call_run(&mut store)

.context("failed to invoke `run` function");



Ok(())

}

the output prints till
I am from Go app

but the tcp connection is not success it gives error
Error: Lookup of host name 'example.com' failed: Netdev not set

I have used wasi preview 2 apis. but network calls are not success.. Is there anything extra I have to do for network configs.

I have given inherit_network() in WasiCtxBuilder object. still it doesn't work.

view this post on Zulip Joel Dice (Aug 07 2024 at 13:57):

Have you tried calling https://docs.rs/wasmtime-wasi/23.0.1/wasmtime_wasi/struct.WasiCtxBuilder.html#method.allow_ip_name_lookup ?

view this post on Zulip celine santosh (Aug 07 2024 at 14:18):

yes , I just tried now

et wasi_ctx = WasiCtxBuilder::new().inherit_stdio().inherit_network().allow_ip_name_lookup(true).build();

still the same error
here is the output
Finished dev profile [unoptimized + debuginfo] target(s) in 0.42s

Running /Users/celinesantosh/Desktop/sem_5/wasmtime_dev/target/debug/celine_app

I am from Go app

Error: Lookup of host name 'example.com' failed: Netdev not set

view this post on Zulip Bailey Hayes (Aug 08 2024 at 18:58):

Golang does not yet have wasip2 support where wasi features like wasi:http have been added. The net/httpmodule is part of the standard library that doesn't know about how to make wasi:http calls. This work is underway, but I can walk you through how to build with tinygo today to get an http request working.

The easiest path to get something working today is to use the wit-bindgen CLI tool to generate go bindings. This tool is written in Rust and supports creating bindings for several languages. By generating bindings to wasi-http, you can write a go program that avoids pulling in modules that don't yet know how to target wasip2 and then build with the tinygo compiler. This works today, example here.

The very soon answer, but not really the recommend approach for folks getting started right now is to use the go native tools we've been building in the BA and golang communities. If you are on the dev branch of tinygo, you can even start building for wasip2 by passing in -target=wasip2. Building tinygo from source locally can be tricky, but you can go to the CI and download an artifact for your architecture. This is dependent on a new tool called wasm-tools-go which is written in go and creates more idiomatic go bindings. Using this tool, I am seeing folks create higher-level bindings that look a lot more like what you'd expect in a go program: https://github.com/hayride-dev/wasi-tinygo/blob/main/main.go

wasmCloud is an open source Cloud Native Computing Foundation (CNCF) project that enables teams to build, manage, and scale polyglot apps across any cloud, K8s, or edge. - wasmCloud/wasmCloud
Example tinygo repository for wasi . Contribute to hayride-dev/wasi-tinygo development by creating an account on GitHub.

Last updated: Nov 22 2024 at 17:03 UTC