File: create_macvlan.rs

package info (click to toggle)
rust-rtnetlink 0.14.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 828 kB
  • sloc: makefile: 2
file content (75 lines) | stat: -rw-r--r-- 2,096 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: MIT
#![cfg(feature = "tokio_socket")]

use futures::stream::TryStreamExt;
use macaddr::MacAddr;
use rtnetlink::{new_connection, Error, Handle};
use std::{env, str::FromStr};

use netlink_packet_route::link::LinkAttribute;

#[tokio::main]
async fn main() -> Result<(), String> {
    let args: Vec<String> = env::args().collect();
    if args.len() != 2 && args.len() != 3 {
        usage();
        return Ok(());
    }
    let link_name = &args[1];
    let mac: Option<Vec<u8>> = if args.len() == 3 {
        let mac_address_arg = (&args[2]).to_string();
        let mac_address = MacAddr::from_str(mac_address_arg.as_str())
            .map_err(|e| format!("{e}"))?;
        Some(mac_address.as_bytes().into())
    } else {
        None
    };

    let (connection, handle, _) = new_connection().unwrap();
    tokio::spawn(connection);

    create_macvlan(handle, link_name.to_string(), mac)
        .await
        .map_err(|e| format!("{e}"))
}

async fn create_macvlan(
    handle: Handle,
    link_name: String,
    mac_address: Option<Vec<u8>>,
) -> Result<(), Error> {
    let mut links = handle.link().get().match_name(link_name.clone()).execute();
    if let Some(link) = links.try_next().await? {
        let mut request = handle.link().add().macvlan(
            "test_macvlan".into(),
            link.header.index,
            4u32, // bridge mode
        );
        if let Some(mac) = mac_address {
            request
                .message_mut()
                .attributes
                .push(LinkAttribute::Address(mac));
        }
        request.execute().await?
    } else {
        println!("no link {link_name} found");
    }
    Ok(())
}

fn usage() {
    eprintln!(
        "usage:
    cargo run --example create_macvlan -- <link name> [mac address]

Note that you need to run this program as root. Instead of running cargo as root,
build the example normally:

    cargo build --example create_macvlan

Then find the binary in the target directory:

    cd target/debug/examples ; sudo ./create_macvlan <link_name> [mac address]"
    );
}