--- fwsync/driver/fwsync_mod.c 2022/06/22 13:01:55 1.1 +++ fwsync/driver/fwsync_mod.c 2022/06/29 19:44:58 1.2 @@ -24,6 +24,9 @@ struct cfg_sync fws_cfg; //static struct sysctl_oid *fws_sysctl_oid, *fws_sysctl_dir; struct task fws_sndpkt_task; +struct taskqueue *fws_tq; +struct mtx fws_mtx_c, fws_mtx_e; +fwsync_sndpkt_t fwsync_sndpkt; SYSCTL_NODE(_net_inet_ip, IFT_FWSYNC, fwsync, CTLFLAG_RW, 0, "IPFW Sync - Sync firewall states"); SYSCTL_INT(_net_inet_ip_fwsync, OID_AUTO, debug, CTLFLAG_RW, &fwsync_debug, 0, "Debug driver"); @@ -31,6 +34,8 @@ SYSCTL_INT(_net_inet_ip_fwsync, OID_AUTO, debug, CTLFL static int fws_fini(void *arg) { + struct fws_sndpkt *p; + DTRACE(); if (!fwsync_hooked) @@ -44,6 +49,20 @@ fws_fini(void *arg) IPFW_DEL_SOPT_HANDLER(1, soc); + if (fws_tq) + taskqueue_free(fws_tq); + + mtx_lock(&fws_mtx_c); + while (!TAILQ_EMPTY(&fwsync_sndpkt)) { + p = TAILQ_FIRST(&fwsync_sndpkt); + TAILQ_REMOVE(&fwsync_sndpkt, p, sp_next); + free(p, M_FWSYNC); + } + mtx_unlock(&fws_mtx_c); + + mtx_destroy(&fws_mtx_c); + mtx_destroy(&fws_mtx_e); + fwsync_hooked = 0; /* sysctl context */ @@ -72,13 +91,30 @@ fws_init(void *arg) return 0; memset(&fws_cfg, 0, sizeof fws_cfg); + memset(&fws_ctx, 0, sizeof fws_ctx); + TAILQ_INIT(&fwsync_sndpkt); + + /* mutexes */ + mtx_init(&fws_mtx_e, "fwsync mtx edge", NULL, MTX_DEF); + mtx_init(&fws_mtx_c, "fwsync mtx collector", NULL, MTX_DEF); + + /* taskqueue */ + TASK_INIT(&fws_sndpkt_task, 0, fwsync_sndpkt_state, &fwsync_sndpkt); + + fws_tq = taskqueue_create("fwsync_tq", M_NOWAIT, taskqueue_thread_enqueue, &fws_tq); + if (!fws_tq) { + printf("Failed to allocate fwsync task queue\n"); + mtx_destroy(&fws_mtx_c); + mtx_destroy(&fws_mtx_e); + return ENOMEM; + } else + taskqueue_start_threads(&fws_tq, 1, PI_NET, "fwsync tq"); + /* sysctl context */ sysctl_ctx_init(&fwsync_sysctl_ctx); IPFW_ADD_SOPT_HANDLER(1, soc); - - memset(&fws_ctx, 0, sizeof fws_ctx); fwsync_hooked = 1; uprintf("Loaded ELWIX %s driver version %d ...\n", DRV_NAME, DRV_VERSION);