From c3e8b06615d7a2d645e64f21b69dcbdbf586a35d Mon Sep 17 00:00:00 2001 From: Eishun Kondoh Date: Fri, 12 Oct 2018 11:09:46 +0900 Subject: [PATCH] work on gobgp --- .../lib/evpn_router/go_bgp/native_utils.ex | 8 ++ examples/evpn_router/mix.exs | 2 +- .../native/{gobgp => evpn_router}/.gitignore | 0 .../native/{gobgp => evpn_router}/Cargo.toml | 12 +-- .../evpn_router/native/evpn_router/build.rs | 3 + .../{vendor => native/evpn_router}/libgobgp.h | 0 .../evpn_router}/libgobgp.so | Bin .../native/evpn_router/src/gobgp_utils.rs | 82 ++++++++++++++++++ .../evpn_router/native/evpn_router/src/lib.rs | 40 +++++++++ examples/evpn_router/native/gobgp/build.rs | 3 - .../native/gobgp/src/gobgp_utils.rs | 40 --------- examples/evpn_router/native/gobgp/src/lib.rs | 27 ------ 12 files changed, 140 insertions(+), 77 deletions(-) create mode 100644 examples/evpn_router/lib/evpn_router/go_bgp/native_utils.ex rename examples/evpn_router/native/{gobgp => evpn_router}/.gitignore (100%) rename examples/evpn_router/native/{gobgp => evpn_router}/Cargo.toml (71%) create mode 100644 examples/evpn_router/native/evpn_router/build.rs rename examples/evpn_router/{vendor => native/evpn_router}/libgobgp.h (100%) rename examples/evpn_router/{vendor => native/evpn_router}/libgobgp.so (100%) mode change 100644 => 100755 create mode 100644 examples/evpn_router/native/evpn_router/src/gobgp_utils.rs create mode 100644 examples/evpn_router/native/evpn_router/src/lib.rs delete mode 100644 examples/evpn_router/native/gobgp/build.rs delete mode 100644 examples/evpn_router/native/gobgp/src/gobgp_utils.rs delete mode 100644 examples/evpn_router/native/gobgp/src/lib.rs diff --git a/examples/evpn_router/lib/evpn_router/go_bgp/native_utils.ex b/examples/evpn_router/lib/evpn_router/go_bgp/native_utils.ex new file mode 100644 index 0000000..261fde7 --- /dev/null +++ b/examples/evpn_router/lib/evpn_router/go_bgp/native_utils.ex @@ -0,0 +1,8 @@ +defmodule EvpnRouter.GoBGP.NativeUtils do + use Rustler, otp_app: :evpn_router, crate: :evpn_router + + @spec get_route_family(String.t()) :: {:ok, non_neg_integer()} + def get_route_family(_p0) do + {:error, :nif_error} + end +end diff --git a/examples/evpn_router/mix.exs b/examples/evpn_router/mix.exs index a3c1de7..9b34b36 100644 --- a/examples/evpn_router/mix.exs +++ b/examples/evpn_router/mix.exs @@ -30,7 +30,7 @@ defmodule EvpnRouter.MixProject do end defp rustler_crates do - [evpn_router: [path: "native/gobgp", mode: rustc_mode(Mix.env)]] + [evpn_router: [path: "native/evpn_router", mode: rustc_mode(Mix.env)]] end defp rustc_mode(:prod), do: :release diff --git a/examples/evpn_router/native/gobgp/.gitignore b/examples/evpn_router/native/evpn_router/.gitignore similarity index 100% rename from examples/evpn_router/native/gobgp/.gitignore rename to examples/evpn_router/native/evpn_router/.gitignore diff --git a/examples/evpn_router/native/gobgp/Cargo.toml b/examples/evpn_router/native/evpn_router/Cargo.toml similarity index 71% rename from examples/evpn_router/native/gobgp/Cargo.toml rename to examples/evpn_router/native/evpn_router/Cargo.toml index a7d968f..b04da08 100644 --- a/examples/evpn_router/native/gobgp/Cargo.toml +++ b/examples/evpn_router/native/evpn_router/Cargo.toml @@ -1,17 +1,17 @@ [package] -name = "gobgp" +name = "evpn_router" version = "0.1.0" -authors = ["HansiHE "] +build = "build.rs" + [lib] -name = "gobgp" +name = "evpn_router" path = "src/lib.rs" crate-type = ["dylib"] -[package.metadata.release] - [dependencies] rustler = { git = "https://github.com/hansihe/rustler", branch = "remove_nif_from_types"} rustler_codegen = { git = "https://github.com/hansihe/rustler", branch = "remove_nif_from_types"} bufstream = "0.1.2" -lazy_static = "1.0.0" \ No newline at end of file +lazy_static = "1.0.0" +libc = "0.2" \ No newline at end of file diff --git a/examples/evpn_router/native/evpn_router/build.rs b/examples/evpn_router/native/evpn_router/build.rs new file mode 100644 index 0000000..44fbb16 --- /dev/null +++ b/examples/evpn_router/native/evpn_router/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rustc-link-search=../evpn_router"); +} diff --git a/examples/evpn_router/vendor/libgobgp.h b/examples/evpn_router/native/evpn_router/libgobgp.h similarity index 100% rename from examples/evpn_router/vendor/libgobgp.h rename to examples/evpn_router/native/evpn_router/libgobgp.h diff --git a/examples/evpn_router/vendor/libgobgp.so b/examples/evpn_router/native/evpn_router/libgobgp.so old mode 100644 new mode 100755 similarity index 100% rename from examples/evpn_router/vendor/libgobgp.so rename to examples/evpn_router/native/evpn_router/libgobgp.so diff --git a/examples/evpn_router/native/evpn_router/src/gobgp_utils.rs b/examples/evpn_router/native/evpn_router/src/gobgp_utils.rs new file mode 100644 index 0000000..eae1278 --- /dev/null +++ b/examples/evpn_router/native/evpn_router/src/gobgp_utils.rs @@ -0,0 +1,82 @@ +/* automatically generated by rust-bindgen */ + +extern crate libc; +use self::libc::{c_void, c_int, c_longlong, c_char, size_t}; + +#[derive(PartialEq, Copy, Clone, Hash, Debug, Default)] +#[repr(C)] +pub struct __BindgenComplex { + pub re: T, + pub im: T, +} + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct max_align_t { + pub __clang_max_align_nonce1: c_longlong, + pub __bindgen_padding_0: u64, + pub __clang_max_align_nonce2: f64, +} + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct _GoString_ { + pub p: *const c_char, + pub n: isize, +} + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct buf { + pub value: *mut c_char, + pub len: c_int, +} + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct path_t { + pub nlri: buf, + pub path_attributes: *mut *mut buf, + pub path_attributes_len: c_int, + pub path_attributes_cap: c_int, +} + +pub type path = path_t; + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct GoInterface { + pub t: *mut c_void, + pub v: *mut c_void, +} + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct GoSlice { + pub data: *mut c_void, + pub len: c_longlong, + pub cap: c_longlong, +} + +#[link(name = "gobgp")] +extern { + pub fn new_path() -> *mut path; + + pub fn free_path(arg1: *mut path); + + pub fn append_path_attribute( + arg1: *mut path, + arg2: c_int, + arg3: *mut c_char, + ) -> c_int; + + pub fn get_path_attribute(arg1: *mut path, arg2: c_int) -> *mut buf; + + pub fn get_route_family(p0: *mut String) -> u32; + + pub fn serialize_path(p0: u32, p1: *mut String) -> *mut path; + + pub fn decode_path(p0: *mut path) -> *mut c_char; + + pub fn decode_capabilities(p0: *mut buf) -> *mut c_char; +} diff --git a/examples/evpn_router/native/evpn_router/src/lib.rs b/examples/evpn_router/native/evpn_router/src/lib.rs new file mode 100644 index 0000000..f8112c9 --- /dev/null +++ b/examples/evpn_router/native/evpn_router/src/lib.rs @@ -0,0 +1,40 @@ +#![allow(bad_style, missing_copy_implementations, improper_ctypes)] + +mod gobgp_utils; +use gobgp_utils::get_route_family; +use gobgp_utils::serialize_path; + +use gobgp_utils::{buf, path_t}; + +#[macro_use] extern crate rustler; +#[macro_use] extern crate lazy_static; + +use rustler::{Env, Term, NifResult, Encoder}; + +mod atoms { + rustler_atoms! { + atom ok; + } +} + +rustler_export_nifs!( + "Elixir.EvpnRouter.GoBGP.NativeUtils", + [("nif_get_route_family", 1, nif_get_route_family), + ("serialize_path", 2, nif_serialize_path)], + None +); + +pub fn nif_get_route_family<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult> { + let mut p0: String = args[0].decode()?; + let family = unsafe { get_route_family(&mut p0) }; + + Ok((atoms::ok(), family).encode(env)) +} + +pub fn nif_serialize_path<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult> { + let mut family: u32 = args[0].decode()?; + let mut path_str: String = args[1].decode()?; + let mut path: path_t = unsafe { serialize_path(&mut family, &mut path_str) } + + Ok((atoms::ok(), )) +} diff --git a/examples/evpn_router/native/gobgp/build.rs b/examples/evpn_router/native/gobgp/build.rs deleted file mode 100644 index a5d047e..0000000 --- a/examples/evpn_router/native/gobgp/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("cargo:rustc-link-search=native=../../vendor/libgobgp.so"); -} diff --git a/examples/evpn_router/native/gobgp/src/gobgp_utils.rs b/examples/evpn_router/native/gobgp/src/gobgp_utils.rs deleted file mode 100644 index 95a0aa7..0000000 --- a/examples/evpn_router/native/gobgp/src/gobgp_utils.rs +++ /dev/null @@ -1,40 +0,0 @@ -/* automatically generated by rust-bindgen */ - -#[derive(PartialEq, Copy, Clone, Hash, Debug, Default)] -#[repr(C)] -pub struct __BindgenComplex { - pub re: T, - pub im: T, -} - -pub type wchar_t = ::std::os::raw::c_int; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct max_align_t { - pub __clang_max_align_nonce1: ::std::os::raw::c_longlong, - pub __bindgen_padding_0: u64, - pub __clang_max_align_nonce2: f64, -} - -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct _GoString_ { - pub p: *const ::std::os::raw::c_char, - pub n: isize, -} - -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct buf { - pub value: *mut ::std::os::raw::c_char, - pub len: ::std::os::raw::c_int, -} - -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct path_t { - pub nlri: buf, - pub path_attributes: *mut *mut buf, - pub path_attributes_len: ::std::os::raw::c_int, - pub path_attributes_cap: ::std::os::raw::c_int, -} diff --git a/examples/evpn_router/native/gobgp/src/lib.rs b/examples/evpn_router/native/gobgp/src/lib.rs deleted file mode 100644 index fe4fb2e..0000000 --- a/examples/evpn_router/native/gobgp/src/lib.rs +++ /dev/null @@ -1,27 +0,0 @@ -#![allow(bad_style, missing_copy_implementations, improper_ctypes)] - -pub mod gobgp_utils; - -#[macro_use] extern crate rustler; -#[macro_use] extern crate lazy_static; - -use rustler::{Env, Term, NifResult, Encoder}; - -mod atoms { - rustler_atoms! { - atom ok; - } -} - -rustler_export_nifs!( - "Elixir.EvpnRouter.GoBGP.NativeUtils", - [("get_route_family", 2, get_route_family)], - None -); - -fn get_route_family<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult> { - let num1: i64 = args[0].decode()?; - let num2: i64 = args[1].decode()?; - - Ok((atoms::ok(), num1 + num2).encode(env)) -}