patches and low-level development discussion
 help / color / mirror / Atom feed
4d222d22823d92f7dc14ad2f02164379769a8323 blob 7848 bytes (raw)

  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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
 
// Copyright 2019 Intel Corporation. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use libc;
use libc::EFD_NONBLOCK;
use std::cmp;
use std::io::Write;
use std::os::unix::prelude::*;
use std::sync::atomic::AtomicUsize;
use std::sync::{Arc, RwLock};
use std::thread;
use std::vec::Vec;
use sync::Mutex;
use sys_util::{error, warn, EventFd, GuestMemory};

use super::cloud_hypervisor::net_util::{MacAddr, MAC_ADDR_LEN};

use super::super::{Queue, VirtioDevice, TYPE_NET};
use super::handler::*;
use super::vu_common_ctrl::*;
use super::{Error, Result};
use crate::{pci::MsixConfig, virtio::Interrupt};
use vhost_rs::vhost_user::message::VhostUserVirtioFeatures;
use vhost_rs::vhost_user::{Master, VhostUserMaster};
use vhost_rs::VhostBackend;
use virtio_bindings::virtio_net;
use virtio_bindings::virtio_ring;

pub struct Net {
    vhost_user_net: Master,
    kill_evt: vmm_sys_util::eventfd::EventFd,
    avail_features: u64,
    acked_features: u64,
    config_space: Vec<u8>,
    queue_sizes: Vec<u16>,
}

impl<'a> Net {
    /// Create a new vhost-user-net device
    pub fn new(mac_addr: MacAddr, vu_cfg: VhostUserConfig<'a>) -> Result<Net> {
        let mut vhost_user_net = Master::connect(vu_cfg.sock, vu_cfg.num_queues as u64)
            .map_err(Error::VhostUserCreateMaster)?;

        let kill_evt =
            vmm_sys_util::eventfd::EventFd::new(EFD_NONBLOCK).map_err(Error::CreateKillEventFd)?;

        // Filling device and vring features VMM supports.
        let mut avail_features = 1 << virtio_net::VIRTIO_NET_F_GUEST_CSUM
            | 1 << virtio_net::VIRTIO_NET_F_CSUM
            | 1 << virtio_net::VIRTIO_NET_F_GUEST_TSO4
            | 1 << virtio_net::VIRTIO_NET_F_GUEST_TSO6
            | 1 << virtio_net::VIRTIO_NET_F_GUEST_ECN
            | 1 << virtio_net::VIRTIO_NET_F_GUEST_UFO
            | 1 << virtio_net::VIRTIO_NET_F_HOST_TSO4
            | 1 << virtio_net::VIRTIO_NET_F_HOST_TSO6
            | 1 << virtio_net::VIRTIO_NET_F_HOST_ECN
            | 1 << virtio_net::VIRTIO_NET_F_HOST_UFO
            | 1 << virtio_net::VIRTIO_NET_F_MRG_RXBUF
            | 1 << virtio_net::VIRTIO_F_NOTIFY_ON_EMPTY
            | 1 << virtio_net::VIRTIO_F_VERSION_1
            | 1 << virtio_ring::VIRTIO_RING_F_EVENT_IDX
            | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits();

        vhost_user_net
            .set_owner()
            .map_err(Error::VhostUserSetOwner)?;

        // Get features from backend, do negotiation to get a feature collection which
        // both VMM and backend support.
        let backend_features = vhost_user_net.get_features().unwrap();
        avail_features &= backend_features;
        // Set features back is required by the vhost crate mechanism, since the
        // later vhost call will check if features is filled in master before execution.
        vhost_user_net
            .set_features(backend_features)
            .map_err(Error::VhostUserSetFeatures)?;

        let mut acked_features = 0;
        if backend_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits() != 0 {
            acked_features |= VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits();
            vhost_user_net
                .get_protocol_features()
                .map_err(Error::VhostUserGetProtocolFeatures)?;
        } else {
            return Err(Error::VhostUserProtocolNotSupport);
        }

        let mut config_space = Vec::with_capacity(MAC_ADDR_LEN);
        unsafe { config_space.set_len(MAC_ADDR_LEN) }
        config_space[..].copy_from_slice(mac_addr.get_bytes());
        avail_features |= 1 << virtio_net::VIRTIO_NET_F_MAC;

        // Send set_vring_base here, since it could tell backends, like OVS + DPDK,
        // how many virt queues to be handled, which backend required to know at early stage.
        for i in 0..vu_cfg.num_queues {
            vhost_user_net
                .set_vring_base(i, 0)
                .map_err(Error::VhostUserSetVringBase)?;
        }

        Ok(Net {
            vhost_user_net,
            kill_evt,
            avail_features,
            acked_features,
            config_space,
            queue_sizes: vec![vu_cfg.queue_size; vu_cfg.num_queues],
        })
    }
}

impl Drop for Net {
    fn drop(&mut self) {
        if let Err(_e) = self.kill_evt.write(1) {
            error!("failed to kill vhost-user-net with error {}", _e);
        }
    }
}

impl VirtioDevice for Net {
    fn keep_fds(&self) -> Vec<RawFd> {
        vec![self.kill_evt.as_raw_fd()]
    }

    fn device_type(&self) -> u32 {
        TYPE_NET as u32
    }

    fn queue_max_sizes(&self) -> &[u16] {
        &self.queue_sizes
    }

    fn features(&self) -> u64 {
        self.avail_features
    }

    fn ack_features(&mut self, mut value: u64) {
        // Check if the guest is ACK'ing a feature that we didn't claim to have.
        let unrequested_features = value & !self.avail_features;
        if unrequested_features != 0 {
            warn!(
                "Received acknowledge request for unknown feature: {:x}",
                value
            );
            // Don't count these features as acked.
            value &= !unrequested_features;
        }
        self.acked_features |= value;
    }

    fn read_config(&self, offset: u64, mut data: &mut [u8]) {
        let config_len = self.config_space.len() as u64;
        if offset >= config_len {
            error!("Failed to read config space");
            return;
        }
        if let Some(end) = offset.checked_add(data.len() as u64) {
            // This write can't fail, offset and end are checked against config_len.
            data.write_all(&self.config_space[offset as usize..cmp::min(end, config_len) as usize])
                .unwrap();
        }
    }

    fn write_config(&mut self, offset: u64, data: &[u8]) {
        let data_len = data.len() as u64;
        let config_len = self.config_space.len() as u64;
        if offset + data_len > config_len {
            error!("Failed to write config space");
            return;
        }
        let (_, right) = self.config_space.split_at_mut(offset as usize);
        right.copy_from_slice(&data[..]);
    }

    fn activate(
        &mut self,
        mem: GuestMemory,
        interrupt_evt: sys_util::EventFd,
        interrupt_resample_evt: sys_util::EventFd,
        msix_config: Option<Arc<Mutex<MsixConfig>>>,
        status: Arc<AtomicUsize>,
        queues: Vec<Queue>,
        queue_evts: Vec<EventFd>,
    ) {
        let handler_kill_evt = match self.kill_evt.try_clone() {
            Ok(v) => v,
            Err(e) => {
                error!("failed creating kill EventFd pair: {}", e);
                return;
            }
        };

        let vu_interrupt_list = match setup_vhost_user(
            &mut self.vhost_user_net,
            &mem,
            queues,
            queue_evts,
            self.acked_features,
        ) {
            Ok(v) => v,
            Err(e) => {
                error!("failed to set up vhost_user: {:?}", e);
                return;
            }
        };

        let vu_epoll_cfg = VhostUserEpollConfig {
            interrupt: Interrupt::new(status, interrupt_evt, interrupt_resample_evt, msix_config),
            kill_evt: handler_kill_evt,
            vu_interrupt_list,
        };

        let _handler_result = thread::Builder::new()
            .name("vhost_user_net".to_string())
            .spawn(move || {
                let mut handler = VhostUserEpollHandler::new(vu_epoll_cfg);
                let result = handler.run();
                if let Err(_e) = result {
                    error!("net worker thread exited with error {:?}!", _e);
                }
            });
        if let Err(_e) = _handler_result {
            error!("vhost-user net thread create failed with error {:?}", _e);
        }
    }
}
debug log:

solving 4d222d22 ...
found 4d222d22 in https://spectrum-os.org/lists/archives/spectrum-devel/20210512170812.192540-2-hi@alyssa.is/

applying [1/1] https://spectrum-os.org/lists/archives/spectrum-devel/20210512170812.192540-2-hi@alyssa.is/
diff --git a/devices/src/virtio/vhost_user/net.rs b/devices/src/virtio/vhost_user/net.rs
new file mode 100644
index 00000000..4d222d22

Checking patch devices/src/virtio/vhost_user/net.rs...
Applied patch devices/src/virtio/vhost_user/net.rs cleanly.

index at:
100644 4d222d22823d92f7dc14ad2f02164379769a8323	devices/src/virtio/vhost_user/net.rs

patches and low-level development discussion

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://spectrum-os.org/lists/archives/spectrum-devel/0 spectrum-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 spectrum-devel spectrum-devel/ https://spectrum-os.org/lists/archives/spectrum-devel \
		public-inbox+spectrum-devel@spectrum-os.org devel@spectrum-os.org
	public-inbox-index spectrum-devel

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntps://spectrum-os.org/inbox.comp.spectrum.devel
	nntp://spectrum-os.org/inbox.comp.spectrum.devel


code repositories for the project(s) associated with this inbox:

	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

AGPL code for this site: https://ftp.qyliss.net/public-inbox/public-inbox-1.6.1-qyliss-v215gfdyjlyh28n105rnspkmk3d7ks86.tar.gz