[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC PATCH 10/12] net/filter-ubpf.c: run the ubpf program to handle netw
From: |
Zhang Chen |
Subject: |
[RFC PATCH 10/12] net/filter-ubpf.c: run the ubpf program to handle network packet |
Date: |
Fri, 17 Jun 2022 15:36:28 +0800 |
Run the loaded userspace ebpf program with the packet.
Signed-off-by: Zhang Chen <chen.zhang@intel.com>
---
net/filter-ubpf.c | 40 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 38 insertions(+), 2 deletions(-)
diff --git a/net/filter-ubpf.c b/net/filter-ubpf.c
index c63a021759..554cc24d8f 100644
--- a/net/filter-ubpf.c
+++ b/net/filter-ubpf.c
@@ -20,6 +20,8 @@
#include "qemu/error-report.h"
#include "trace.h"
#include "ebpf/ubpf.h"
+#include "colo.h"
+#include "util.h"
#define TYPE_FILTER_UBPF "filter-ubpf"
OBJECT_DECLARE_SIMPLE_TYPE(FiliterUbpfState, FILTER_UBPF)
@@ -38,9 +40,43 @@ static ssize_t filter_ubpf_receive_iov(NetFilterState *nf,
int iovcnt,
NetPacketSent *sent_cb)
{
- /* TODO: handle packet by loaded userspace ebpf program */
+ FiliterUbpfState *s = FILTER_UBPF(nf);
+ size_t size;
+ char *buf;
+ Packet *pkt = NULL;
+ uint64_t result;
+
+ size = iov_size(iov, iovcnt);
+ if (!size) {
+ return 0;
+ }
+
+ buf = g_malloc(size);
+ if (unlikely(iov_to_buf(iov, iovcnt, 0, buf, size) != size)) {
+ g_free(buf);
+ return 0;
+ }
+
+ pkt = packet_new_nocopy(buf, size, 0);
- return 0;
+ if (parse_packet_early(pkt)) {
+ packet_destroy(pkt, NULL);
+ pkt = NULL;
+ return 0;
+ }
+
+ if (s->ip_mode) {
+ result = qemu_ubpf_run_once(&s->ubpf, pkt->ip, sizeof(struct ip));
+ } else {
+ result = qemu_ubpf_run_once(&s->ubpf, pkt->data, pkt->size);
+ }
+
+ /* If result == 1, means trigger the ebpf program rules */
+ if (result) {
+ return -1;
+ } else {
+ return 0;
+ }
}
static void filter_ubpf_cleanup(NetFilterState *nf)
--
2.25.1
- [RFC PATCH 02/12] meson: Add ubpf build config and misc, (continued)
- [RFC PATCH 02/12] meson: Add ubpf build config and misc, Zhang Chen, 2022/06/17
- [RFC PATCH 03/12] ebpf/uBPF: Introduce userspace ebpf data structure, Zhang Chen, 2022/06/17
- [RFC PATCH 05/12] ebpf/uBPF: Add qemu_prepare_ubpf to load ebpf binary, Zhang Chen, 2022/06/17
- [RFC PATCH 04/12] ebpf/uBPF: Introduce ubpf initialize functions, Zhang Chen, 2022/06/17
- [RFC PATCH 06/12] ebpf/uBPF: Add qemu_ubpf_run_once excute real ebpf program, Zhang Chen, 2022/06/17
- [RFC PATCH 07/12] net/filter: Introduce filter-ubpf module, Zhang Chen, 2022/06/17
- [RFC PATCH 09/12] softmmu/vl.c: Add filter-ubpf for netdev as other netfilters, Zhang Chen, 2022/06/17
- [RFC PATCH 08/12] qapi: Add FilterUbpfProperties and qemu-options, Zhang Chen, 2022/06/17
- [RFC PATCH 10/12] net/filter-ubpf.c: run the ubpf program to handle network packet,
Zhang Chen <=
- [RFC PATCH 11/12] docs/devel: Add userspace-ebpf.rst, Zhang Chen, 2022/06/17
- [RFC PATCH 12/12] test/qtest: Add ubpf basic test case, Zhang Chen, 2022/06/17
- Re: [RFC PATCH 00/12] Introduce QEMU userspace ebpf support, Andrew Melnichenko, 2022/06/29