To me, netgraph felt a bit tricky when I started out the first time:
Code:
# create ngeth0 interface
$ ngctl mkpeer . eiface hook ether
# list available netgraph interfaces
$ ngctl list
There are 2 total nodes:
Name: ngeth0 Type: eiface ID: 00000003 Num hooks: 0 # <--- ethernet interface we just created
Name: ngctl235 Type: socket ID: 00000004 Num hooks: 0
# create unnamed bridge interface and attach ngeth0 interface to it
ngctl mkpeer ngeth0: bridge ether link0
# list available netgraph interfaces
$ ngctl list
There are 3 total nodes:
Name: ngeth0 Type: eiface ID: 00000003 Num hooks: 1
Name: <unnamed> Type: bridge ID: 00000007 Num hooks: 1 # <--- unnamed bridge interface we just created
Name: ngctl5710 Type: socket ID: 00000008 Num hooks: 0
# rename unnamed bridge interface to ngbr0
ngctl name ngeth0:ether ngbr0
# list available netgraph interfaces
$ ngctl list
There are 3 total nodes:
Name: ngeth0 Type: eiface ID: 00000003 Num hooks: 1
Name: ngbr0 Type: bridge ID: 00000007 Num hooks: 1 # <--- renamed bridge interface ngbr0, will only be visible by ngctl, not by using ifconfig
Name: ngctl8395 Type: socket ID: 0000000a Num hooks: 0
# assign IP addr to ngeth0 interface
$ ifconfig ngeth0 inet 172.16.254.1/24
Now we have to configure bhyve to use ngbr0 as a switch, I prefer the vm-bhyve framework.
Code:
# configure ngbr0 as switch for our vm named vmTest
$ vm configure vmTest
[...]
# default configuration
network0_type="virtio-net"
# configure ngbr0 to be used as switch
network0_switch="ngbr0"
[...]
# display switch configuration
$ vm switch list
NAME TYPE IFACE ADDRESS PRIVATE MTU VLAN PORTS
ngbr0 netraph netgraph,path=ngbr0:,peerhook=link2 n/a n/a n/a n/a n/a # <--- our 1st vm will be attached with ngbr0:link2, 2nd vm ngbr0:link3, ...
# launch vmTest
$ vm start vmTest
So back to netgraph, our vmTest interface will appear as netgraph socket outside the vm, but inside the vm it will appear as ethernet interface.
Code:
# list available netgraph interfaces
$ ngctl list
There are 5 total nodes:
Name: <unnamed> Type: socket ID: 00000011 Num hooks: 1 # <--- our 1st vm vmTest netgraph socket
Name: ngeth0 Type: eiface ID: 00000002 Num hooks: 1
Name: ngbr0 Type: bridge ID: 00000004 Num hooks: 3
Name: ngctl91481 Type: socket ID: 0000001b Num hooks: 0
# list interfaces attached to ngbr0 interface
$ ntctl show ngbr0:
Name: ngbr0 Type: bridge ID: 00000004 Num hooks: 2
Local hook Peer name Peer type Peer ID Peer hook
---------- --------- --------- ------- ---------
link2 <unnamed> socket 00000011 vmlink # <--- we can here see our vm is attached by socket to our ngbr0 interface
link0 ngeth0 eiface 00000002 ether
# rename unnamed vmTest socket
$ ngctl name ngbr0:link2 vmTest
# list available netgraph interfaces
$ ngctl list
There are 5 total nodes:
Name: vmTest Type: socket ID: 00000011 Num hooks: 1 # <--- our 1st vm vmTest netgraph socket
Name: ngeth0 Type: eiface ID: 00000002 Num hooks: 1
Name: ngbr0 Type: bridge ID: 00000004 Num hooks: 3
Name: ngctl91481 Type: socket ID: 0000001b Num hooks: 0
# list interfaces attached to ngbr0 interface
$ ntctl show ngbr0:
Name: ngbr0 Type: bridge ID: 00000004 Num hooks: 2
Local hook Peer name Peer type Peer ID Peer hook
---------- --------- --------- ------- ---------
link2 vmTest socket 00000011 vmlink # <--- we can here see our vm is attached by socket to our ngbr0 interface
link0 ngeth0 eiface 00000002 ether
Left to do is to assign an IP addr inside the vm vmTest like 172.16.254.100/24 and you are good to go.
Links which helped me to figure this all out:
https://people.freebsd.org/~julian/netgraph.html
man netgraph
man vm
Using Netgraph for FreeBSD's Bhyve Networking - Jun 15, 2022
If you are interested in
vale as well, please see
FreeBSD Forums: Bhyve HyperVisor Vale Networking Interface No Carrier .
I can recommend
FreeBSD SR-IOV as well, which is what I'm using right now.
Have fun!
PS: I'm doing this all, using FreeBSD 14.2-RELEASE.