network/macsec: use hash_ops with destructor

This commit is contained in:
Yu Watanabe
2025-04-13 02:20:24 +09:00
parent c64c71fbe5
commit 86aa7cb4b8

View File

@@ -50,6 +50,11 @@ static ReceiveAssociation* macsec_receive_association_free(ReceiveAssociation *c
DEFINE_SECTION_CLEANUP_FUNCTIONS(ReceiveAssociation, macsec_receive_association_free);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
receive_association_hash_ops_by_section,
ConfigSection, config_section_hash_func, config_section_compare_func,
ReceiveAssociation, macsec_receive_association_free);
static int macsec_receive_association_new_static(MACsec *s, const char *filename, unsigned section_line, ReceiveAssociation **ret) {
_cleanup_(config_section_freep) ConfigSection *n = NULL;
_cleanup_(macsec_receive_association_freep) ReceiveAssociation *c = NULL;
@@ -80,12 +85,11 @@ static int macsec_receive_association_new_static(MACsec *s, const char *filename
.sa = SECURITY_ASSOCIATION_NULL,
};
r = ordered_hashmap_ensure_put(&s->receive_associations_by_section, &config_section_hash_ops, c->section, c);
r = ordered_hashmap_ensure_put(&s->receive_associations_by_section, &receive_association_hash_ops_by_section, c->section, c);
if (r < 0)
return r;
*ret = TAKE_PTR(c);
return 0;
}
@@ -108,6 +112,16 @@ static ReceiveChannel* macsec_receive_channel_free(ReceiveChannel *c) {
DEFINE_SECTION_CLEANUP_FUNCTIONS(ReceiveChannel, macsec_receive_channel_free);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
receive_channel_hash_ops,
uint64_t, uint64_hash_func, uint64_compare_func,
ReceiveChannel, macsec_receive_channel_free);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
receive_channel_hash_ops_by_section,
ConfigSection, config_section_hash_func, config_section_compare_func,
ReceiveChannel, macsec_receive_channel_free);
static int macsec_receive_channel_new(MACsec *s, uint64_t sci, ReceiveChannel **ret) {
ReceiveChannel *c;
@@ -152,12 +166,11 @@ static int macsec_receive_channel_new_static(MACsec *s, const char *filename, un
c->section = TAKE_PTR(n);
r = ordered_hashmap_ensure_put(&s->receive_channels_by_section, &config_section_hash_ops, c->section, c);
r = ordered_hashmap_ensure_put(&s->receive_channels_by_section, &receive_channel_hash_ops_by_section, c->section, c);
if (r < 0)
return r;
*ret = TAKE_PTR(c);
return 0;
}
@@ -176,6 +189,11 @@ static TransmitAssociation* macsec_transmit_association_free(TransmitAssociation
DEFINE_SECTION_CLEANUP_FUNCTIONS(TransmitAssociation, macsec_transmit_association_free);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
transmit_association_hash_ops_by_section,
ConfigSection, config_section_hash_func, config_section_compare_func,
TransmitAssociation, macsec_transmit_association_free);
static int macsec_transmit_association_new_static(MACsec *s, const char *filename, unsigned section_line, TransmitAssociation **ret) {
_cleanup_(config_section_freep) ConfigSection *n = NULL;
_cleanup_(macsec_transmit_association_freep) TransmitAssociation *a = NULL;
@@ -206,12 +224,11 @@ static int macsec_transmit_association_new_static(MACsec *s, const char *filenam
.sa = SECURITY_ASSOCIATION_NULL,
};
r = ordered_hashmap_ensure_put(&s->transmit_associations_by_section, &config_section_hash_ops, a->section, a);
r = ordered_hashmap_ensure_put(&s->transmit_associations_by_section, &transmit_association_hash_ops_by_section, a->section, a);
if (r < 0)
return r;
*ret = TAKE_PTR(a);
return 0;
}
@@ -1015,7 +1032,7 @@ static int macsec_receive_channel_verify(ReceiveChannel *c) {
"Ignoring [MACsecReceiveChannel] section from line %u",
c->section->filename, c->section->line);
r = ordered_hashmap_ensure_put(&c->macsec->receive_channels, &uint64_hash_ops, &c->sci.as_uint64, c);
r = ordered_hashmap_ensure_put(&c->macsec->receive_channels, &receive_channel_hash_ops, &c->sci.as_uint64, c);
if (r == -ENOMEM)
return log_oom();
if (r == -EEXIST)
@@ -1105,7 +1122,7 @@ static int macsec_receive_association_verify(ReceiveAssociation *a) {
if (r < 0)
return log_oom();
r = ordered_hashmap_ensure_put(&a->macsec->receive_channels, &uint64_hash_ops, &new_channel->sci.as_uint64, new_channel);
r = ordered_hashmap_ensure_put(&a->macsec->receive_channels, &receive_channel_hash_ops, &new_channel->sci.as_uint64, new_channel);
if (r == -ENOMEM)
return log_oom();
if (r < 0)
@@ -1200,10 +1217,10 @@ static void macsec_init(NetDev *netdev) {
static void macsec_done(NetDev *netdev) {
MACsec *v = MACSEC(netdev);
ordered_hashmap_free_with_destructor(v->receive_channels, macsec_receive_channel_free);
ordered_hashmap_free_with_destructor(v->receive_channels_by_section, macsec_receive_channel_free);
ordered_hashmap_free_with_destructor(v->transmit_associations_by_section, macsec_transmit_association_free);
ordered_hashmap_free_with_destructor(v->receive_associations_by_section, macsec_receive_association_free);
ordered_hashmap_free(v->receive_channels);
ordered_hashmap_free(v->receive_channels_by_section);
ordered_hashmap_free(v->transmit_associations_by_section);
ordered_hashmap_free(v->receive_associations_by_section);
}
const NetDevVTable macsec_vtable = {