Skip to content

Commit cdc5f12

Browse files
ssqrewenlingz
authored andcommitted
dm: virtio-net: clean up assert
This patch is to clean up assert from virtio-net. Tracked-On: #3252 Signed-off-by: Jie Deng <jie.deng@intel.com> Reviewed-by: Shuo A Liu <shuo.a.liu@intel.com>
1 parent b001596 commit cdc5f12

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

devicemodel/hw/pci/virtio/virtio_net.c

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <stdlib.h>
3434
#include <string.h>
3535
#include <unistd.h>
36-
#include <assert.h>
3736
#include <openssl/md5.h>
3837
#include <pthread.h>
3938
#include <sys/ioctl.h>
@@ -345,11 +344,17 @@ rx_iov_trim(struct iovec *iov, int *niov, int tlen)
345344
struct iovec *riov;
346345

347346
/* XXX short-cut: assume first segment is >= tlen */
348-
assert(iov[0].iov_len >= tlen);
347+
if (iov[0].iov_len < tlen) {
348+
WPRINTF(("vtnet: rx_iov_trim: iov_len=%lu, tlen=%d\n", iov[0].iov_len, tlen));
349+
return NULL;
350+
}
349351

350352
iov[0].iov_len -= tlen;
351353
if (iov[0].iov_len == 0) {
352-
assert(*niov > 1);
354+
if (*niov <= 1) {
355+
WPRINTF(("vtnet: rx_iov_trim: *niov=%d\n", *niov));
356+
return NULL;
357+
}
353358
*niov -= 1;
354359
riov = &iov[1];
355360
} else {
@@ -373,7 +378,10 @@ virtio_net_tap_rx(struct virtio_net *net)
373378
/*
374379
* Should never be called without a valid tap fd
375380
*/
376-
assert(net->tapfd != -1);
381+
if (net->tapfd == -1) {
382+
WPRINTF(("vtnet: tapfd == -1\n"));
383+
return;
384+
}
377385

378386
/*
379387
* But, will be called when the rx ring hasn't yet
@@ -410,14 +418,18 @@ virtio_net_tap_rx(struct virtio_net *net)
410418
* Get descriptor chain.
411419
*/
412420
n = vq_getchain(vq, &idx, iov, VIRTIO_NET_MAXSEGS, NULL);
413-
assert(n >= 1 && n <= VIRTIO_NET_MAXSEGS);
414-
421+
if (n < 1 || n > VIRTIO_NET_MAXSEGS) {
422+
WPRINTF(("vtnet: virtio_net_tap_rx: vq_getchain = %d\n", n));
423+
return;
424+
}
415425
/*
416426
* Get a pointer to the rx header, and use the
417427
* data immediately following it for the packet buffer.
418428
*/
419429
vrx = iov[0].iov_base;
420430
riov = rx_iov_trim(iov, &n, net->rx_vhdrlen);
431+
if (riov == NULL)
432+
return;
421433

422434
len = readv(net->tapfd, riov, n);
423435

@@ -495,7 +507,10 @@ virtio_net_proctx(struct virtio_net *net, struct virtio_vq_info *vq)
495507
* up two lengths: packet length and transfer length.
496508
*/
497509
n = vq_getchain(vq, &idx, iov, VIRTIO_NET_MAXSEGS, NULL);
498-
assert(n >= 1 && n <= VIRTIO_NET_MAXSEGS);
510+
if (n < 1 || n > VIRTIO_NET_MAXSEGS) {
511+
WPRINTF(("vtnet: virtio_net_proctx: vq_getchain = %d\n", n));
512+
return;
513+
}
499514
plen = 0;
500515
tlen = iov[0].iov_len;
501516
for (i = 1; i < n; i++) {
@@ -537,18 +552,15 @@ virtio_net_tx_thread(void *param)
537552
{
538553
struct virtio_net *net = param;
539554
struct virtio_vq_info *vq = &net->queues[VIRTIO_NET_TXQ];
540-
int error;
541555

542556
/*
543557
* Let us wait till the tx queue pointers get initialised &
544558
* first tx signaled
545559
*/
546560
pthread_mutex_lock(&net->tx_mtx);
547561

548-
while (!net->closing && !vq_ring_ready(vq)) {
549-
error = pthread_cond_wait(&net->tx_cond, &net->tx_mtx);
550-
assert(error == 0);
551-
}
562+
while (!net->closing && !vq_ring_ready(vq))
563+
pthread_cond_wait(&net->tx_cond, &net->tx_mtx);
552564

553565
if (net->closing) {
554566
WPRINTF(("vtnet tx thread closing...\n"));
@@ -572,8 +584,8 @@ virtio_net_tx_thread(void *param)
572584
if (!net->resetting && vq_has_descs(vq))
573585
break;
574586

575-
error = pthread_cond_wait(&net->tx_cond, &net->tx_mtx);
576-
assert(error == 0);
587+
pthread_cond_wait(&net->tx_cond, &net->tx_mtx);
588+
577589
if (net->closing) {
578590
WPRINTF(("vtnet tx thread closing...\n"));
579591
pthread_mutex_unlock(&net->tx_mtx);
@@ -901,7 +913,10 @@ virtio_net_cfgwrite(void *vdev, int offset, int size, uint32_t value)
901913
void *ptr;
902914

903915
if (offset < 6) {
904-
assert(offset + size <= 6);
916+
if (offset + size > 6) {
917+
DPRINTF(("vtnet: wrong params offset=%d, size=%d, ignore write mac address\n\r", offset, size));
918+
return -1;
919+
}
905920
/*
906921
* The driver is allowed to change the MAC address
907922
*/

0 commit comments

Comments
 (0)