CMM: add multicast group offload module

Port NXP ASK multicast control plane to FreeBSD. CMM maintains a
shadow table of multicast groups and forwards add/remove/update
commands to CDX via FCI for hardware multicast replication.

CDX kernel-side support (cdx_mc_freebsd.c) was already ported;
this adds the userspace CMM module and cmmctl mc4/mc6 commands.
This commit is contained in:
Tomaz Zaman 2026-03-13 21:22:59 +01:00
parent ed28c8320d
commit 824daa1634
12 changed files with 1348 additions and 4 deletions

View file

@ -30,7 +30,8 @@ SRCS= cmm.c \
cmm_tunnel.c \
cmm_ctrl.c \
cmm_wifi.c \
cmm_pppoe.c
cmm_pppoe.c \
cmm_mcast.c
CFLAGS+= -I${.CURDIR}/../fci_lib
CFLAGS+= -I${.CURDIR}/../fci

View file

@ -23,7 +23,7 @@ CMM_SRCS= cmm.c cmm_conn.c cmm_route.c cmm_neigh.c \
cmm_itf.c cmm_rtsock.c cmm_fe.c cmm_offload.c \
cmm_lagg.c cmm_vlan.c cmm_ipsec.c cmm_tunnel.c cmm_deny.c \
cmm_socket.c cmm_bridge.c cmm_ctrl.c cmm_l2tp.c \
cmm_wifi.c cmm_pppoe.c
cmm_wifi.c cmm_pppoe.c cmm_mcast.c
CMM_OBJS= ${CMM_SRCS:.c=.o}
PROG= cmm

View file

@ -42,6 +42,7 @@
#include "cmm_bridge.h"
#include "cmm_wifi.h"
#include "cmm_pppoe.h"
#include "cmm_mcast.h"
#include "cmm_ctrl.h"
#include "pf_notify.h"
#include "libfci.h"
@ -315,6 +316,7 @@ main(int argc, char *argv[])
cmm_bridge_init(g);
cmm_wifi_init(g);
cmm_pppoe_init(g);
cmm_mcast_init();
if (cmm_ipsec_init() < 0)
goto out;
@ -490,6 +492,7 @@ out:
cmm_ctrl_fini(g);
cmm_sa_flush_all(g);
cmm_conn_deregister_all(g);
cmm_mcast_fini();
cmm_wifi_fini(g);
cmm_bridge_fini(g);
cmm_socket_fini(g);

View file

@ -23,6 +23,7 @@
#include "cmm_tunnel.h"
#include "cmm_l2tp.h"
#include "cmm_socket.h"
#include "cmm_mcast.h"
/* kqueue udata tags — cast to void * for EV_SET */
#define CMM_UDATA_CTRL_LISTEN ((void *)(uintptr_t)1)
@ -368,6 +369,27 @@ cmm_ctrl_dispatch(struct cmm_global *g, int client_fd)
}
}
/*
* Multicast commands — CMM maintains a shadow table and
* forwards to CDX via FCI. Not FPP whitelist passthrough.
*/
switch (hdr.cmd) {
case FPP_CMD_MC4_MULTICAST:
cmm_mcast_ctrl_mc4(g, client_fd, cmd_buf, hdr.len);
return;
case FPP_CMD_MC4_RESET:
cmm_mcast_ctrl_mc4_reset(g, client_fd);
return;
case FPP_CMD_MC6_MULTICAST:
cmm_mcast_ctrl_mc6(g, client_fd, cmd_buf, hdr.len);
return;
case FPP_CMD_MC6_RESET:
cmm_mcast_ctrl_mc6_reset(g, client_fd);
return;
default:
break;
}
/*
* CMM-internal commands (above FPP range).
* These are CMM's own control protocol, not FPP passthrough,

View file

@ -20,6 +20,7 @@
#include "cmm_route.h"
#include "cmm_itf.h"
#include "cmm_bridge.h"
#include "cmm_mcast.h"
/* Conntrack command flags (from Linux forward_engine.c) */
#define CTCMD_FLAGS_ORIG_DISABLED (1 << 0)
@ -43,6 +44,14 @@ cmm_fe_reset(struct cmm_global *g)
if (rc != 0)
cmm_print(CMM_LOG_WARN, "fe: IPv6 reset failed: %d", rc);
rc = fci_write(g->fci_handle, FPP_CMD_MC4_RESET, 0, NULL);
if (rc != 0)
cmm_print(CMM_LOG_WARN, "fe: MC4 reset failed: %d", rc);
rc = fci_write(g->fci_handle, FPP_CMD_MC6_RESET, 0, NULL);
if (rc != 0)
cmm_print(CMM_LOG_WARN, "fe: MC6 reset failed: %d", rc);
return (0);
}

View file

@ -34,6 +34,7 @@
#include "cmm_bridge.h"
#include "cmm_wifi.h"
#include "cmm_pppoe.h"
#include "cmm_mcast.h"
#include "cmm_lagg.h"
static struct list_head itf_hash[ITF_HASH_SIZE];
@ -560,12 +561,14 @@ cmm_itf_handle_ifinfo(struct cmm_global *g, void *msg, int msglen)
itf->mtu = ifm->ifm_data.ifi_mtu;
}
/* Notify LAGG, VLAN, tunnel, WiFi, PPPoE, and bridge modules of flag changes */
/* Notify LAGG, VLAN, tunnel, WiFi, PPPoE, multicast, and bridge modules */
cmm_lagg_notify(g, itf);
cmm_vlan_notify(g, itf);
cmm_tunnel_notify(g, itf);
cmm_wifi_notify(g, itf);
cmm_pppoe_notify(g, itf);
cmm_mcast_itf_update(g, itf->ifname,
(itf->flags & IFF_UP) ? 1 : 0);
/* If this is a bridge or a potential bridge member, rescan */
if ((itf->itf_flags & ITF_F_BRIDGE) ||

754
fastpath/cmm/cmm_mcast.c Normal file
View file

@ -0,0 +1,754 @@
/*
* cmm_mcast.c — Multicast group offload
*
* Maintains a userspace shadow table of multicast groups and
* forwards add/remove/update commands to CDX via FCI for
* hardware multicast replication.
*
* Ported from NXP ASK module_mcast.c / module_mc4.c / module_mc6.c.
*
* Copyright 2026 Mono Technologies Inc.
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include "cmm.h"
#include "cmm_mcast.h"
#include "cmm_ctrl.h"
#include "cmm_itf.h"
#include "libfci.h"
static struct list_head mc_table[MC_HASH_SIZE];
static inline unsigned int
mc_hash_v4(uint32_t dst_addr)
{
return (ntohl(dst_addr) & (MC_HASH_SIZE - 1));
}
static inline unsigned int
mc_hash_v6(const uint32_t *dst_addr)
{
return (ntohl(dst_addr[3]) & (MC_HASH_SIZE - 1));
}
static struct cmm_mcast_entry *
mc_find(const void *cmd_payload, sa_family_t family)
{
struct cmm_mcast_entry *mc;
struct list_head *pos;
unsigned int h;
uint32_t src[4], dst[4];
int alen;
if (family == AF_INET) {
const struct cmm_mc4_cmd *c = cmd_payload;
src[0] = c->src_addr;
dst[0] = c->dst_addr;
alen = 4;
h = mc_hash_v4(dst[0]);
} else {
const struct cmm_mc6_cmd *c = cmd_payload;
memcpy(src, c->src_addr, 16);
memcpy(dst, c->dst_addr, 16);
alen = 16;
h = mc_hash_v6(dst);
}
for (pos = list_first(&mc_table[h]); pos != &mc_table[h];
pos = list_next(pos)) {
mc = container_of(pos, struct cmm_mcast_entry, list);
if (mc->family == family &&
memcmp(mc->src_addr, src, alen) == 0 &&
memcmp(mc->dst_addr, dst, alen) == 0)
return (mc);
}
return (NULL);
}
/*
* Send MC4 FCI command to CDX for a specific listener interface.
* Builds the wire-format MC4Command with only the listeners that
* match `ifname` and whose program state differs from `action`.
*/
static int
mc_send_mc4(struct cmm_global *g, unsigned short action,
struct cmm_mcast_entry *mc, const char *ifname)
{
struct cmm_mc4_cmd cmd;
int i, j, rc;
uint8_t program;
program = (action == MC_ACTION_ADD) ? 1 : 0;
memset(&cmd, 0, sizeof(cmd));
cmd.action = action;
cmd.mode = mc->mode;
cmd.queue = mc->queue;
cmd.src_addr_mask = mc->src_mask_len;
cmd.src_addr = mc->src_addr[0];
cmd.dst_addr = mc->dst_addr[0];
strlcpy(cmd.input_device, mc->input_device,
sizeof(cmd.input_device));
for (i = 0, j = 0; i < mc->num_output && j < 5; i++) {
if ((program ^ mc->l_program[i]) &&
ifname != NULL &&
strncmp(mc->listener[i].output_device, ifname,
IFNAMSIZ) == 0) {
memcpy(&cmd.output_list[j], &mc->listener[i],
sizeof(struct cmm_mc_listener));
mc->l_program[i] = program;
j++;
}
}
cmd.num_output = j;
if (j == 0)
return (0);
rc = fci_write(g->fci_handle, FPP_CMD_MC4_MULTICAST,
sizeof(cmd), (unsigned short *)&cmd);
if (rc == FPP_ERR_MC_ENTRY_NOT_FOUND && action == MC_ACTION_REMOVE)
return (0);
if (rc != 0)
cmm_print(CMM_LOG_WARN,
"mcast: mc4 send failed: %d (action=%d)", rc, action);
return (rc);
}
/*
* Send MC6 FCI command to CDX.
*/
static int
mc_send_mc6(struct cmm_global *g, unsigned short action,
struct cmm_mcast_entry *mc, const char *ifname)
{
struct cmm_mc6_cmd cmd;
int i, j, rc;
uint8_t program;
program = (action == MC_ACTION_ADD) ? 1 : 0;
memset(&cmd, 0, sizeof(cmd));
cmd.action = action;
cmd.mode = mc->mode;
cmd.queue = mc->queue;
cmd.src_mask_len = mc->src_mask_len;
memcpy(cmd.src_addr, mc->src_addr, 16);
memcpy(cmd.dst_addr, mc->dst_addr, 16);
strlcpy(cmd.input_device, mc->input_device,
sizeof(cmd.input_device));
for (i = 0, j = 0; i < mc->num_output && j < 5; i++) {
if ((program ^ mc->l_program[i]) &&
ifname != NULL &&
strncmp(mc->listener[i].output_device, ifname,
IFNAMSIZ) == 0) {
memcpy(&cmd.output_list[j], &mc->listener[i],
sizeof(struct cmm_mc_listener));
mc->l_program[i] = program;
j++;
}
}
cmd.num_output = j;
if (j == 0)
return (0);
rc = fci_write(g->fci_handle, FPP_CMD_MC6_MULTICAST,
sizeof(cmd), (unsigned short *)&cmd);
if (rc == FPP_ERR_MC_ENTRY_NOT_FOUND && action == MC_ACTION_REMOVE)
return (0);
if (rc != 0)
cmm_print(CMM_LOG_WARN,
"mcast: mc6 send failed: %d (action=%d)", rc, action);
return (rc);
}
/*
* Add a listener to a group. Creates the group if it doesn't exist.
* Returns the group entry, or NULL on failure.
*/
static struct cmm_mcast_entry *
mc_add(struct cmm_mcast_entry *mc, const void *cmd_payload,
const struct cmm_mc_listener *listener, int num_listeners,
uint8_t program, sa_family_t family)
{
unsigned int h;
int i, j, k, dup;
int add_group;
add_group = (mc == NULL);
if (mc == NULL) {
mc = calloc(1, sizeof(*mc));
if (mc == NULL)
return (NULL);
}
if (family == AF_INET) {
const struct cmm_mc4_cmd *c = cmd_payload;
if (add_group) {
mc->mode = c->mode;
mc->queue = c->queue;
mc->src_addr[0] = c->src_addr;
mc->dst_addr[0] = c->dst_addr;
mc->src_mask_len = c->src_addr_mask;
mc->family = AF_INET;
strlcpy(mc->input_device, c->input_device,
sizeof(mc->input_device));
}
dup = 0;
for (i = mc->num_output, j = 0;
i < MC_MAX_LISTENERS && j < num_listeners; i++, j++) {
for (k = 0; k < i; k++) {
if (memcmp(listener[j].uc_mac,
mc->listener[k].uc_mac,
ETHER_ADDR_LEN) == 0 &&
strncmp(listener[j].output_device,
mc->listener[k].output_device,
IFNAMSIZ) == 0) {
dup++;
break;
}
}
if (k == i) {
memcpy(&mc->listener[i], &listener[j],
sizeof(struct cmm_mc_listener));
mc->l_program[i] = program;
}
}
mc->num_output = i - dup;
} else {
const struct cmm_mc6_cmd *c = cmd_payload;
if (add_group) {
mc->mode = c->mode;
mc->queue = c->queue;
memcpy(mc->src_addr, c->src_addr, 16);
memcpy(mc->dst_addr, c->dst_addr, 16);
mc->src_mask_len = c->src_mask_len;
mc->family = AF_INET6;
strlcpy(mc->input_device, c->input_device,
sizeof(mc->input_device));
}
dup = 0;
for (i = mc->num_output, j = 0;
i < MC_MAX_LISTENERS && j < num_listeners; i++, j++) {
for (k = 0; k < i; k++) {
if (memcmp(listener[j].uc_mac,
mc->listener[k].uc_mac,
ETHER_ADDR_LEN) == 0 &&
strncmp(listener[j].output_device,
mc->listener[k].output_device,
IFNAMSIZ) == 0) {
dup++;
break;
}
}
if (k == i) {
memcpy(&mc->listener[i], &listener[j],
sizeof(struct cmm_mc_listener));
mc->l_program[i] = program;
}
}
mc->num_output = i - dup;
}
if (add_group) {
if (family == AF_INET)
h = mc_hash_v4(mc->dst_addr[0]);
else
h = mc_hash_v6(mc->dst_addr);
list_add(&mc_table[h], &mc->list);
cmm_print(CMM_LOG_INFO,
"mcast: group added (%s, %d listeners)",
family == AF_INET ? "IPv4" : "IPv6",
mc->num_output);
}
return (mc);
}
/*
* Remove listener(s) from a group. If no listeners remain,
* deletes the group from the hash table and frees it.
*/
static int
mc_remove(struct cmm_mcast_entry *mc,
const struct cmm_mc_listener *listener, int num_listeners)
{
int i, j;
for (i = 0; i < num_listeners; i++) {
for (j = 0; j < mc->num_output; j++) {
if (listener[i].uc_bit) {
if (!mc->listener[j].uc_bit)
continue;
if (memcmp(listener[i].uc_mac,
mc->listener[j].uc_mac,
ETHER_ADDR_LEN) != 0)
continue;
} else {
if (mc->listener[j].uc_bit)
continue;
if (strncmp(listener[i].output_device,
mc->listener[j].output_device,
IFNAMSIZ) != 0)
continue;
}
/* Found — compact the array */
memmove(&mc->listener[j], &mc->listener[j + 1],
(mc->num_output - (j + 1)) *
sizeof(struct cmm_mc_listener));
for (; j < mc->num_output - 1; j++)
mc->l_program[j] = mc->l_program[j + 1];
mc->num_output--;
break;
}
}
if (mc->num_output == 0) {
cmm_print(CMM_LOG_INFO, "mcast: group removed (%s)",
mc->family == AF_INET ? "IPv4" : "IPv6");
list_del(&mc->list);
free(mc);
}
return (0);
}
/*
* Update listener properties in-place.
*/
static int
mc_update(struct cmm_mcast_entry *mc,
const struct cmm_mc_listener *listener, int num_listeners)
{
int i, j;
for (j = 0; j < num_listeners; j++) {
for (i = 0; i < mc->num_output; i++) {
if (memcmp(listener[j].uc_mac,
mc->listener[i].uc_mac,
ETHER_ADDR_LEN) == 0 &&
strncmp(listener[j].output_device,
mc->listener[i].output_device,
IFNAMSIZ) == 0) {
memcpy(&mc->listener[i], &listener[j],
sizeof(struct cmm_mc_listener));
break;
}
}
}
return (0);
}
static void
mc_reset(sa_family_t family)
{
struct cmm_mcast_entry *mc;
struct list_head *pos, *tmp;
int i;
for (i = 0; i < MC_HASH_SIZE; i++) {
pos = list_first(&mc_table[i]);
while (pos != &mc_table[i]) {
tmp = list_next(pos);
mc = container_of(pos, struct cmm_mcast_entry, list);
if (mc->family == family) {
list_del(&mc->list);
free(mc);
}
pos = tmp;
}
}
}
/*
* Helper: send ctrl response back to client.
*/
static void
ctrl_respond(int client_fd, int16_t rc, const void *data, uint16_t data_len)
{
struct cmm_ctrl_resp resp;
resp.rc = rc;
resp.len = data_len;
write(client_fd, &resp, sizeof(resp));
if (data_len > 0 && data != NULL)
write(client_fd, data, data_len);
}
/*
* Check if an interface is up and registered with CMM.
* Returns 1 if the interface is suitable for multicast offload.
*/
static int
itf_is_programmed(const char *ifname)
{
struct cmm_interface *itf;
int ifindex;
ifindex = if_nametoindex(ifname);
if (ifindex == 0)
return (0);
itf = cmm_itf_find_by_index(ifindex);
if (itf == NULL)
return (0);
/* Interface must be up */
if (!(itf->flags & IFF_UP))
return (0);
return (1);
}
int
cmm_mcast_init(void)
{
int i;
for (i = 0; i < MC_HASH_SIZE; i++)
list_head_init(&mc_table[i]);
cmm_print(CMM_LOG_INFO, "mcast: initialized");
return (0);
}
void
cmm_mcast_fini(void)
{
struct cmm_mcast_entry *mc;
struct list_head *pos, *tmp;
int i;
for (i = 0; i < MC_HASH_SIZE; i++) {
pos = list_first(&mc_table[i]);
while (pos != &mc_table[i]) {
tmp = list_next(pos);
mc = container_of(pos, struct cmm_mcast_entry, list);
list_del(&mc->list);
free(mc);
pos = tmp;
}
}
}
void
cmm_mcast_ctrl_mc4(struct cmm_global *g, int client_fd,
uint16_t *cmd_buf, uint16_t cmd_len)
{
struct cmm_mc4_cmd *entry = (struct cmm_mc4_cmd *)cmd_buf;
struct cmm_mc_listener *listener;
struct cmm_mcast_entry *mc;
uint16_t resp_buf[CMM_CTRL_MAX_PAYLOAD / 2];
unsigned short resp_len;
int program, rc;
if (cmd_len < sizeof(struct cmm_mc4_cmd)) {
ctrl_respond(client_fd, CMM_CTRL_ERR_BAD_LEN, NULL, 0);
return;
}
/* Query commands pass straight through to CDX */
if (entry->action == FPP_ACTION_QUERY ||
entry->action == FPP_ACTION_QUERY_CONT) {
if (g->fci_handle == NULL) {
ctrl_respond(client_fd, CMM_CTRL_ERR_NO_FCI, NULL, 0);
return;
}
resp_len = sizeof(resp_buf);
rc = fci_cmd(g->fci_handle, FPP_CMD_MC4_MULTICAST,
cmd_buf, cmd_len, resp_buf, &resp_len);
if (rc < 0) {
ctrl_respond(client_fd, CMM_CTRL_ERR_FCI_FAIL,
NULL, 0);
return;
}
ctrl_respond(client_fd,
(resp_len >= 2) ? (int16_t)resp_buf[0] : 0,
resp_buf, resp_len);
return;
}
if (entry->num_output > 5) {
ctrl_respond(client_fd, FPP_ERR_MC_MAX_LISTENERS, NULL, 0);
return;
}
listener = (struct cmm_mc_listener *)((uint8_t *)cmd_buf +
sizeof(struct cmm_mc4_cmd));
/* Check if the output interface is ready for offload */
program = 1;
if (entry->num_output > 0 &&
!itf_is_programmed(listener[0].output_device))
program = 0;
mc = mc_find(entry, AF_INET);
switch (entry->action) {
case MC_ACTION_ADD:
if (mc != NULL && mc->num_output >= MC_MAX_LISTENERS) {
ctrl_respond(client_fd, FPP_ERR_MC_MAX_LISTENERS,
NULL, 0);
return;
}
mc = mc_add(mc, entry, listener, entry->num_output,
program, AF_INET);
if (mc == NULL) {
ctrl_respond(client_fd, -1, NULL, 0);
return;
}
break;
case MC_ACTION_REMOVE:
if (mc == NULL) {
ctrl_respond(client_fd, FPP_ERR_MC_ENTRY_NOT_FOUND,
NULL, 0);
return;
}
mc_remove(mc, listener, entry->num_output);
break;
case MC_ACTION_UPDATE:
if (mc == NULL) {
ctrl_respond(client_fd, FPP_ERR_MC_ENTRY_NOT_FOUND,
NULL, 0);
return;
}
mc->mode = entry->mode;
mc->queue = entry->queue;
mc_update(mc, listener, entry->num_output);
break;
case MC_ACTION_REMOVE_LOCAL:
if (mc != NULL) {
list_del(&mc->list);
free(mc);
}
ctrl_respond(client_fd, 0, NULL, 0);
return;
default:
ctrl_respond(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD, NULL, 0);
return;
}
/* Forward to CDX via FCI */
if (g->fci_handle == NULL) {
ctrl_respond(client_fd, CMM_CTRL_ERR_NO_FCI, NULL, 0);
return;
}
resp_len = sizeof(resp_buf);
rc = fci_cmd(g->fci_handle, FPP_CMD_MC4_MULTICAST,
cmd_buf, cmd_len, resp_buf, &resp_len);
if (rc < 0) {
ctrl_respond(client_fd, CMM_CTRL_ERR_FCI_FAIL, NULL, 0);
return;
}
ctrl_respond(client_fd,
(resp_len >= 2) ? (int16_t)resp_buf[0] : 0,
resp_buf, resp_len);
}
void
cmm_mcast_ctrl_mc6(struct cmm_global *g, int client_fd,
uint16_t *cmd_buf, uint16_t cmd_len)
{
struct cmm_mc6_cmd *entry = (struct cmm_mc6_cmd *)cmd_buf;
struct cmm_mc_listener *listener;
struct cmm_mcast_entry *mc;
uint16_t resp_buf[CMM_CTRL_MAX_PAYLOAD / 2];
unsigned short resp_len;
int program, rc;
if (cmd_len < sizeof(struct cmm_mc6_cmd)) {
ctrl_respond(client_fd, CMM_CTRL_ERR_BAD_LEN, NULL, 0);
return;
}
/* Query commands pass straight through to CDX */
if (entry->action == FPP_ACTION_QUERY ||
entry->action == FPP_ACTION_QUERY_CONT) {
if (g->fci_handle == NULL) {
ctrl_respond(client_fd, CMM_CTRL_ERR_NO_FCI, NULL, 0);
return;
}
resp_len = sizeof(resp_buf);
rc = fci_cmd(g->fci_handle, FPP_CMD_MC6_MULTICAST,
cmd_buf, cmd_len, resp_buf, &resp_len);
if (rc < 0) {
ctrl_respond(client_fd, CMM_CTRL_ERR_FCI_FAIL,
NULL, 0);
return;
}
ctrl_respond(client_fd,
(resp_len >= 2) ? (int16_t)resp_buf[0] : 0,
resp_buf, resp_len);
return;
}
if (entry->num_output > 5) {
ctrl_respond(client_fd, FPP_ERR_MC_MAX_LISTENERS, NULL, 0);
return;
}
listener = (struct cmm_mc_listener *)((uint8_t *)cmd_buf +
sizeof(struct cmm_mc6_cmd));
program = 1;
if (entry->num_output > 0 &&
!itf_is_programmed(listener[0].output_device))
program = 0;
mc = mc_find(entry, AF_INET6);
switch (entry->action) {
case MC_ACTION_ADD:
if (mc != NULL && mc->num_output >= MC_MAX_LISTENERS) {
ctrl_respond(client_fd, FPP_ERR_MC_MAX_LISTENERS,
NULL, 0);
return;
}
mc = mc_add(mc, entry, listener, entry->num_output,
program, AF_INET6);
if (mc == NULL) {
ctrl_respond(client_fd, -1, NULL, 0);
return;
}
break;
case MC_ACTION_REMOVE:
if (mc == NULL) {
ctrl_respond(client_fd, FPP_ERR_MC_ENTRY_NOT_FOUND,
NULL, 0);
return;
}
mc_remove(mc, listener, entry->num_output);
break;
case MC_ACTION_UPDATE:
if (mc == NULL) {
ctrl_respond(client_fd, FPP_ERR_MC_ENTRY_NOT_FOUND,
NULL, 0);
return;
}
mc->mode = entry->mode;
mc->queue = entry->queue;
mc_update(mc, listener, entry->num_output);
break;
case MC_ACTION_REMOVE_LOCAL:
if (mc != NULL) {
list_del(&mc->list);
free(mc);
}
ctrl_respond(client_fd, 0, NULL, 0);
return;
default:
ctrl_respond(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD, NULL, 0);
return;
}
/* Forward to CDX via FCI */
if (g->fci_handle == NULL) {
ctrl_respond(client_fd, CMM_CTRL_ERR_NO_FCI, NULL, 0);
return;
}
resp_len = sizeof(resp_buf);
rc = fci_cmd(g->fci_handle, FPP_CMD_MC6_MULTICAST,
cmd_buf, cmd_len, resp_buf, &resp_len);
if (rc < 0) {
ctrl_respond(client_fd, CMM_CTRL_ERR_FCI_FAIL, NULL, 0);
return;
}
ctrl_respond(client_fd,
(resp_len >= 2) ? (int16_t)resp_buf[0] : 0,
resp_buf, resp_len);
}
void
cmm_mcast_ctrl_mc4_reset(struct cmm_global *g, int client_fd)
{
int rc = 0;
if (g->fci_handle != NULL)
rc = fci_write(g->fci_handle, FPP_CMD_MC4_RESET, 0, NULL);
if (rc == 0)
mc_reset(AF_INET);
ctrl_respond(client_fd, rc, NULL, 0);
}
void
cmm_mcast_ctrl_mc6_reset(struct cmm_global *g, int client_fd)
{
int rc = 0;
if (g->fci_handle != NULL)
rc = fci_write(g->fci_handle, FPP_CMD_MC6_RESET, 0, NULL);
if (rc == 0)
mc_reset(AF_INET6);
ctrl_respond(client_fd, rc, NULL, 0);
}
void
cmm_mcast_itf_update(struct cmm_global *g, const char *ifname, int is_up)
{
struct cmm_mcast_entry *mc;
struct list_head *pos;
unsigned short action;
int i, j;
if (g->fci_handle == NULL)
return;
action = is_up ? MC_ACTION_ADD : MC_ACTION_REMOVE;
for (i = 0; i < MC_HASH_SIZE; i++) {
for (pos = list_first(&mc_table[i]); pos != &mc_table[i];
pos = list_next(pos)) {
mc = container_of(pos, struct cmm_mcast_entry, list);
for (j = 0; j < mc->num_output; j++) {
if (strncmp(mc->listener[j].output_device,
ifname, IFNAMSIZ) != 0)
continue;
if (mc->family == AF_INET)
mc_send_mc4(g, action, mc, ifname);
else
mc_send_mc6(g, action, mc, ifname);
break;
}
}
}
}

137
fastpath/cmm/cmm_mcast.h Normal file
View file

@ -0,0 +1,137 @@
/*
* cmm_mcast.h — Multicast group offload
*
* Maintains a shadow table of multicast groups and their
* listeners. Programs CDX hardware multicast replication
* via FCI CMD_MC4/MC6_MULTICAST.
*
* Copyright 2026 Mono Technologies Inc.
* SPDX-License-Identifier: BSD-2-Clause
*/
#ifndef CMM_MCAST_H
#define CMM_MCAST_H
#include <net/if.h>
#include <net/ethernet.h>
#include <stdint.h>
#include "cmm.h"
/* Hash table sizes — match CDX kernel module */
#define MC_HASH_SIZE 32
#define MC_MAX_LISTENERS 8 /* MC_MAX_LISTENERS_PER_GROUP in CDX */
/* Actions (match CDX_MC_ACTION_*) */
#define MC_ACTION_ADD 0
#define MC_ACTION_REMOVE 1
#define MC_ACTION_UPDATE 2
#define MC_ACTION_REMOVE_LOCAL 11
/*
* FPP command codes for multicast (match CMD_MC4/MC6_* in
* cdx_cmdhandler.h). These are the FCI wire protocol codes
* that CDX expects.
*/
#define FPP_CMD_MC4_MULTICAST 0x0701
#define FPP_CMD_MC4_RESET 0x0702
#define FPP_CMD_MC6_MULTICAST 0x0703
#define FPP_CMD_MC6_RESET 0x0704
/*
* Listener — one output port for a multicast group.
* Wire format matches MC4Output/MC6Output in dpa_control_mc.h.
*/
struct cmm_mc_listener {
uint32_t timer;
char output_device[IFNAMSIZ];
uint8_t shaper_mask;
uint8_t uc_bit:1,
q_bit:1,
rsvd:6;
uint8_t uc_mac[ETHER_ADDR_LEN];
uint8_t queue;
char new_output_device[IFNAMSIZ];
uint8_t ifbit:1,
rsvd1:7;
uint8_t padding[2];
} __packed;
/*
* Multicast group entry — CMM shadow table.
* Tracks one (src, dst) multicast group with up to
* MC_MAX_LISTENERS output ports.
*/
struct cmm_mcast_entry {
struct list_head list;
uint8_t family; /* AF_INET or AF_INET6 */
uint8_t mode:1, /* 0=routed, 1=bridged */
queue:5,
rsvd:2;
uint8_t src_mask_len;
uint32_t src_addr[4]; /* IPv4 in [0], IPv6 full */
uint32_t dst_addr[4];
uint8_t num_output;
uint8_t l_program[MC_MAX_LISTENERS];
char input_device[IFNAMSIZ];
struct cmm_mc_listener listener[MC_MAX_LISTENERS];
};
/*
* MC4 command payload — wire format for CMD_MC4_MULTICAST.
* Must match MC4Command in dpa_control_mc.h exactly.
*/
struct cmm_mc4_cmd {
uint16_t action;
uint8_t src_addr_mask;
uint8_t mode:1,
queue:5,
rsvd:2;
uint32_t src_addr;
uint32_t dst_addr;
uint32_t num_output;
char input_device[IFNAMSIZ];
struct cmm_mc_listener output_list[5];
} __packed;
/*
* MC6 command payload — wire format for CMD_MC6_MULTICAST.
* Must match MC6Command in dpa_control_mc.h exactly.
*/
struct cmm_mc6_cmd {
uint16_t action;
uint8_t mode:1,
queue:5,
rsvd:2;
uint8_t src_mask_len;
uint32_t src_addr[4];
uint32_t dst_addr[4];
uint32_t num_output;
char input_device[IFNAMSIZ];
struct cmm_mc_listener output_list[5];
} __packed;
/* Initialize / finalize multicast module */
int cmm_mcast_init(void);
void cmm_mcast_fini(void);
/*
* Process MC4/MC6 control commands from the control socket.
* Handles add/remove/update locally, then forwards to CDX via FCI.
* Query commands pass straight through to CDX.
*/
void cmm_mcast_ctrl_mc4(struct cmm_global *g, int client_fd,
uint16_t *cmd_buf, uint16_t cmd_len);
void cmm_mcast_ctrl_mc6(struct cmm_global *g, int client_fd,
uint16_t *cmd_buf, uint16_t cmd_len);
void cmm_mcast_ctrl_mc4_reset(struct cmm_global *g, int client_fd);
void cmm_mcast_ctrl_mc6_reset(struct cmm_global *g, int client_fd);
/*
* Re-program multicast groups when an interface changes state.
* Called from cmm_itf.c on link state changes.
*/
void cmm_mcast_itf_update(struct cmm_global *g, const char *ifname,
int is_up);
#endif /* CMM_MCAST_H */

View file

@ -21,7 +21,7 @@ CMM_DIR= ${SRCDIR}/cmm
CTL_SRCS= cmmctl.c cmmctl_qm.c cmmctl_tunnel.c cmmctl_stat.c cmmctl_ff.c \
cmmctl_socket.c cmmctl_bridge.c cmmctl_pktcap.c cmmctl_prf.c \
cmmctl_natpt.c cmmctl_icc.c cmmctl_l2tp.c cmmctl_macvlan.c \
cmmctl_tx.c
cmmctl_tx.c cmmctl_mcast.c
CTL_OBJS= ${CTL_SRCS:.c=.o}
PROG= cmmctl

View file

@ -164,6 +164,8 @@ static struct cmmctl_cmd commands[] = {
{ "l2tp", cmmctl_l2tp_main, "L2TP tunnel interfaces" },
{ "macvlan", cmmctl_macvlan_main, "Virtual MAC interfaces" },
{ "tx", cmmctl_tx_main, "TX DSCP to VLAN PCP mapping" },
{ "mc4", cmmctl_mc4_main, "IPv4 multicast offload" },
{ "mc6", cmmctl_mc6_main, "IPv6 multicast offload" },
{ NULL, NULL, NULL }
};

View file

@ -39,5 +39,7 @@ int cmmctl_icc_main(int argc, char **argv, int fd);
int cmmctl_l2tp_main(int argc, char **argv, int fd);
int cmmctl_macvlan_main(int argc, char **argv, int fd);
int cmmctl_tx_main(int argc, char **argv, int fd);
int cmmctl_mc4_main(int argc, char **argv, int fd);
int cmmctl_mc6_main(int argc, char **argv, int fd);
#endif /* CMMCTL_H */

View file

@ -0,0 +1,411 @@
/*
* cmmctl_mcast.c — Multicast group offload control
*
* cmmctl mc4 add <dst_ip> <src_ip> <input_if> <output_if>
* cmmctl mc4 remove <dst_ip> <src_ip> <output_if>
* cmmctl mc4 query
* cmmctl mc4 reset
* cmmctl mc6 add <dst_ip6> <src_ip6> <input_if> <output_if>
* cmmctl mc6 remove <dst_ip6> <src_ip6> <output_if>
* cmmctl mc6 query
* cmmctl mc6 reset
*
* Copyright 2026 Mono Technologies Inc.
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cmmctl.h"
/* FPP command codes — must match cmm_mcast.h */
#define FPP_CMD_MC4_MULTICAST 0x0701
#define FPP_CMD_MC4_RESET 0x0702
#define FPP_CMD_MC6_MULTICAST 0x0703
#define FPP_CMD_MC6_RESET 0x0704
/* Actions */
#define MC_ACTION_ADD 0
#define MC_ACTION_REMOVE 1
/* MC4Output/MC6Output — wire format, must match dpa_control_mc.h */
struct mc_output {
uint32_t timer;
char output_device[IFNAMSIZ];
uint8_t shaper_mask;
uint8_t uc_bit:1,
q_bit:1,
rsvd:6;
uint8_t uc_mac[ETHER_ADDR_LEN];
uint8_t queue;
char new_output_device[IFNAMSIZ];
uint8_t ifbit:1,
rsvd1:7;
uint8_t padding[2];
} __packed;
/* MC4Command wire format */
struct mc4_cmd {
uint16_t action;
uint8_t src_addr_mask;
uint8_t mode:1,
queue:5,
rsvd:2;
uint32_t src_addr;
uint32_t dst_addr;
uint32_t num_output;
char input_device[IFNAMSIZ];
struct mc_output output_list[5];
} __packed;
/* MC6Command wire format */
struct mc6_cmd {
uint16_t action;
uint8_t mode:1,
queue:5,
rsvd:2;
uint8_t src_mask_len;
uint32_t src_addr[4];
uint32_t dst_addr[4];
uint32_t num_output;
char input_device[IFNAMSIZ];
struct mc_output output_list[5];
} __packed;
static void
mc4_usage(void)
{
fprintf(stderr,
"usage: cmmctl mc4 add <dst_ip> <src_ip> <input_if> <output_if>\n"
" cmmctl mc4 remove <dst_ip> <src_ip> <output_if>\n"
" cmmctl mc4 query\n"
" cmmctl mc4 reset\n");
}
static void
mc6_usage(void)
{
fprintf(stderr,
"usage: cmmctl mc6 add <dst_ip6> <src_ip6> <input_if> <output_if>\n"
" cmmctl mc6 remove <dst_ip6> <src_ip6> <output_if>\n"
" cmmctl mc6 query\n"
" cmmctl mc6 reset\n");
}
static void
mc4_print_entry(const struct mc4_cmd *cmd)
{
char dst[INET_ADDRSTRLEN], src[INET_ADDRSTRLEN];
int i;
inet_ntop(AF_INET, &cmd->dst_addr, dst, sizeof(dst));
inet_ntop(AF_INET, &cmd->src_addr, src, sizeof(src));
printf(" group %s src %s input %s mode=%s queue=%u\n",
dst, src,
cmd->input_device[0] ? cmd->input_device : "(none)",
cmd->mode ? "bridged" : "routed",
cmd->queue);
for (i = 0; i < (int)cmd->num_output && i < 5; i++) {
printf(" listener[%d]: %s", i,
cmd->output_list[i].output_device);
if (cmd->output_list[i].uc_bit)
printf(" uc_mac=%02x:%02x:%02x:%02x:%02x:%02x",
cmd->output_list[i].uc_mac[0],
cmd->output_list[i].uc_mac[1],
cmd->output_list[i].uc_mac[2],
cmd->output_list[i].uc_mac[3],
cmd->output_list[i].uc_mac[4],
cmd->output_list[i].uc_mac[5]);
printf("\n");
}
}
static void
mc6_print_entry(const struct mc6_cmd *cmd)
{
char dst[INET6_ADDRSTRLEN], src[INET6_ADDRSTRLEN];
int i;
inet_ntop(AF_INET6, cmd->dst_addr, dst, sizeof(dst));
inet_ntop(AF_INET6, cmd->src_addr, src, sizeof(src));
printf(" group %s src %s input %s mode=%s queue=%u\n",
dst, src,
cmd->input_device[0] ? cmd->input_device : "(none)",
cmd->mode ? "bridged" : "routed",
cmd->queue);
for (i = 0; i < (int)cmd->num_output && i < 5; i++) {
printf(" listener[%d]: %s", i,
cmd->output_list[i].output_device);
if (cmd->output_list[i].uc_bit)
printf(" uc_mac=%02x:%02x:%02x:%02x:%02x:%02x",
cmd->output_list[i].uc_mac[0],
cmd->output_list[i].uc_mac[1],
cmd->output_list[i].uc_mac[2],
cmd->output_list[i].uc_mac[3],
cmd->output_list[i].uc_mac[4],
cmd->output_list[i].uc_mac[5]);
printf("\n");
}
}
int
cmmctl_mc4_main(int argc, char **argv, int fd)
{
struct mc4_cmd cmd;
int16_t rc;
uint16_t resp_len;
uint8_t resp_buf[512];
if (argc < 1) {
mc4_usage();
return (1);
}
if (strcmp(argv[0], "add") == 0) {
if (argc < 5) {
mc4_usage();
return (1);
}
memset(&cmd, 0, sizeof(cmd));
cmd.action = MC_ACTION_ADD;
if (inet_pton(AF_INET, argv[1], &cmd.dst_addr) != 1) {
fprintf(stderr, "invalid dst address: %s\n", argv[1]);
return (1);
}
if (inet_pton(AF_INET, argv[2], &cmd.src_addr) != 1) {
fprintf(stderr, "invalid src address: %s\n", argv[2]);
return (1);
}
strlcpy(cmd.input_device, argv[3], sizeof(cmd.input_device));
cmd.num_output = 1;
strlcpy(cmd.output_list[0].output_device, argv[4],
sizeof(cmd.output_list[0].output_device));
if (ctrl_command(fd, FPP_CMD_MC4_MULTICAST,
&cmd, sizeof(cmd), &rc, NULL, NULL) < 0)
return (1);
if (rc != 0) {
fprintf(stderr, "mc4 add failed: rc=%d\n", rc);
return (1);
}
printf("mc4 group added\n");
return (0);
} else if (strcmp(argv[0], "remove") == 0) {
if (argc < 4) {
mc4_usage();
return (1);
}
memset(&cmd, 0, sizeof(cmd));
cmd.action = MC_ACTION_REMOVE;
if (inet_pton(AF_INET, argv[1], &cmd.dst_addr) != 1) {
fprintf(stderr, "invalid dst address: %s\n", argv[1]);
return (1);
}
if (inet_pton(AF_INET, argv[2], &cmd.src_addr) != 1) {
fprintf(stderr, "invalid src address: %s\n", argv[2]);
return (1);
}
cmd.num_output = 1;
strlcpy(cmd.output_list[0].output_device, argv[3],
sizeof(cmd.output_list[0].output_device));
if (ctrl_command(fd, FPP_CMD_MC4_MULTICAST,
&cmd, sizeof(cmd), &rc, NULL, NULL) < 0)
return (1);
if (rc != 0) {
fprintf(stderr, "mc4 remove failed: rc=%d\n", rc);
return (1);
}
printf("mc4 group removed\n");
return (0);
} else if (strcmp(argv[0], "query") == 0) {
memset(&cmd, 0, sizeof(cmd));
cmd.action = 6; /* FPP_ACTION_QUERY */
resp_len = sizeof(resp_buf);
if (ctrl_command(fd, FPP_CMD_MC4_MULTICAST,
&cmd, sizeof(cmd), &rc, resp_buf, &resp_len) < 0)
return (1);
if (rc != 0 && rc != 2 /* ERR_UNKNOWN_COMMAND */) {
printf("MC4 groups:\n");
mc4_print_entry((struct mc4_cmd *)resp_buf);
/* Continue querying */
while (rc == 0) {
cmd.action = 7; /* FPP_ACTION_QUERY_CONT */
resp_len = sizeof(resp_buf);
if (ctrl_command(fd, FPP_CMD_MC4_MULTICAST,
&cmd, sizeof(cmd), &rc, resp_buf,
&resp_len) < 0)
return (1);
if (rc == 0)
mc4_print_entry(
(struct mc4_cmd *)resp_buf);
}
} else if (rc == 0) {
printf("MC4 groups:\n");
mc4_print_entry((struct mc4_cmd *)resp_buf);
cmd.action = 7;
while (1) {
resp_len = sizeof(resp_buf);
if (ctrl_command(fd, FPP_CMD_MC4_MULTICAST,
&cmd, sizeof(cmd), &rc, resp_buf,
&resp_len) < 0)
return (1);
if (rc != 0)
break;
mc4_print_entry(
(struct mc4_cmd *)resp_buf);
}
} else {
printf("No MC4 groups\n");
}
return (0);
} else if (strcmp(argv[0], "reset") == 0) {
if (ctrl_command(fd, FPP_CMD_MC4_RESET,
NULL, 0, &rc, NULL, NULL) < 0)
return (1);
if (rc != 0) {
fprintf(stderr, "mc4 reset failed: rc=%d\n", rc);
return (1);
}
printf("mc4 groups reset\n");
return (0);
}
mc4_usage();
return (1);
}
int
cmmctl_mc6_main(int argc, char **argv, int fd)
{
struct mc6_cmd cmd;
int16_t rc;
uint16_t resp_len;
uint8_t resp_buf[512];
if (argc < 1) {
mc6_usage();
return (1);
}
if (strcmp(argv[0], "add") == 0) {
if (argc < 5) {
mc6_usage();
return (1);
}
memset(&cmd, 0, sizeof(cmd));
cmd.action = MC_ACTION_ADD;
if (inet_pton(AF_INET6, argv[1], cmd.dst_addr) != 1) {
fprintf(stderr, "invalid dst address: %s\n", argv[1]);
return (1);
}
if (inet_pton(AF_INET6, argv[2], cmd.src_addr) != 1) {
fprintf(stderr, "invalid src address: %s\n", argv[2]);
return (1);
}
strlcpy(cmd.input_device, argv[3], sizeof(cmd.input_device));
cmd.num_output = 1;
strlcpy(cmd.output_list[0].output_device, argv[4],
sizeof(cmd.output_list[0].output_device));
if (ctrl_command(fd, FPP_CMD_MC6_MULTICAST,
&cmd, sizeof(cmd), &rc, NULL, NULL) < 0)
return (1);
if (rc != 0) {
fprintf(stderr, "mc6 add failed: rc=%d\n", rc);
return (1);
}
printf("mc6 group added\n");
return (0);
} else if (strcmp(argv[0], "remove") == 0) {
if (argc < 4) {
mc6_usage();
return (1);
}
memset(&cmd, 0, sizeof(cmd));
cmd.action = MC_ACTION_REMOVE;
if (inet_pton(AF_INET6, argv[1], cmd.dst_addr) != 1) {
fprintf(stderr, "invalid dst address: %s\n", argv[1]);
return (1);
}
if (inet_pton(AF_INET6, argv[2], cmd.src_addr) != 1) {
fprintf(stderr, "invalid src address: %s\n", argv[2]);
return (1);
}
cmd.num_output = 1;
strlcpy(cmd.output_list[0].output_device, argv[3],
sizeof(cmd.output_list[0].output_device));
if (ctrl_command(fd, FPP_CMD_MC6_MULTICAST,
&cmd, sizeof(cmd), &rc, NULL, NULL) < 0)
return (1);
if (rc != 0) {
fprintf(stderr, "mc6 remove failed: rc=%d\n", rc);
return (1);
}
printf("mc6 group removed\n");
return (0);
} else if (strcmp(argv[0], "query") == 0) {
memset(&cmd, 0, sizeof(cmd));
cmd.action = 6; /* FPP_ACTION_QUERY */
resp_len = sizeof(resp_buf);
if (ctrl_command(fd, FPP_CMD_MC6_MULTICAST,
&cmd, sizeof(cmd), &rc, resp_buf, &resp_len) < 0)
return (1);
if (rc == 0) {
printf("MC6 groups:\n");
mc6_print_entry((struct mc6_cmd *)resp_buf);
cmd.action = 7;
while (1) {
resp_len = sizeof(resp_buf);
if (ctrl_command(fd, FPP_CMD_MC6_MULTICAST,
&cmd, sizeof(cmd), &rc, resp_buf,
&resp_len) < 0)
return (1);
if (rc != 0)
break;
mc6_print_entry(
(struct mc6_cmd *)resp_buf);
}
} else {
printf("No MC6 groups\n");
}
return (0);
} else if (strcmp(argv[0], "reset") == 0) {
if (ctrl_command(fd, FPP_CMD_MC6_RESET,
NULL, 0, &rc, NULL, NULL) < 0)
return (1);
if (rc != 0) {
fprintf(stderr, "mc6 reset failed: rc=%d\n", rc);
return (1);
}
printf("mc6 groups reset\n");
return (0);
}
mc6_usage();
return (1);
}