Hi Lucas, Rosemarie,

On 11/9/26 8:50 PM, Rosemarie O'Riorden wrote:
> Hi,
> 
> On 11/9/26 2:07 PM, Lucas Vargas Dias wrote:
>> Hi Dumitru and Rosemarie,
>>
>> Thanks for the review and applying the patch!
> 
> +1, thanks Dumitru!
> 
>>
>> On the hash check in ecmp_hash_count_get(): two different values can
>> indeed share a bucket, but 
>> hmap_next_with_hash__() compares the full node->hash, not only the
>> bucket, so nodes with 
>> different values are skipped. In this helper the value we insert is the
>> key itself, so any node 
>> HMAP_FOR_EACH_WITH_HASH returns already matches, and the 
>> check is redundant, as Rosemarie said. The same goes for
>> ecmp_id_slot_get(). 
>> It doesn't change the behavior, though, so I'm fine keeping it.
>> I can send a small follow-up to drop it if you prefer.
>>
>> I'll send a backport shortly.
>>
>> Regards,
>> Lucas

...

>>     >> +
>>     >> +static struct ecmp_id_slot *
>>     >> +ecmp_id_slot_get(struct hmap *slots, uint16_t id)
>>     >> +{
>>     >> +    struct ecmp_id_slot *slot;
>>     >> +    HMAP_FOR_EACH_WITH_HASH (slot, hmap_node, id, slots) {
>>     >> +        if (slot->id == id) {
>>     >> +            return slot;
>>     >> +        }
>>     >> +    }
>>     >> +    slot = xzalloc(sizeof *slot);
>>     >> +    slot->id = id;
>>     >> +    hmap_insert(slots, &slot->hmap_node, id);
>>     >> +    return slot;
>>     >> +}
>>     >> +
>>     >> +struct ecmp_hash_count {
>>     >> +    struct hmap_node hmap_node;
>>     >> +    uint32_t hash;
>>     >> +    size_t n;
>>     >> +};
>>     >> +
>>     >> +static struct ecmp_hash_count *
>>     >> +ecmp_hash_count_get(struct hmap *counts, uint32_t hash)
>>     >> +{
>>     >> +    struct ecmp_hash_count *count;
>>     >> +    HMAP_FOR_EACH_WITH_HASH (count, hmap_node, hash, counts) {
>>     >> +        if (count->hash == hash) {
>>     >
>>     > Btw, this if-condition will always be true. HMAP_FOR_EACH_WITH_HASH
>>     > already checks for the hash match. So this is doing that comparison
>>     > twice. So I'd recommend removing the if-condition and just returning
>>     > count when a node is found.
>>
>>     I don't think you're right, Rosemarie.  It's a bit confusing that
>>     we're using the hash value as hashmap "hash" too.  But you could have
>>     two ecmp hash values that "hash" to the same bucket.
>>
>>     See:
>>
>>     /* Returns the first node in 'hmap' with the given 'hash', or a null
>>     pointer if
>>      * no nodes have that hash value. */
>>     static inline struct hmap_node *
>>     hmap_first_with_hash(const struct hmap *hmap, size_t hash)
>>     {
>>         return hmap_next_with_hash__(hmap->buckets[hash & hmap->mask],
>>     hash);
>>     }
>>
>>     So I actually think Lucas' version is the correct one.
> 
> I was going to say what Lucas said above. The difference between
> in_bucket and with_hash is that in_bucket goes through the whole bucket,
> no matter if there are collisions, i.e. two different hashes that hash
> into the same bucket becuase of the mask, while with_hash iterates over
> the bucket AND checks that the full hashes match. So you're right about
> the chance of collisions, but HMAP_FOR_EACH_WITH_HASH is literally
> designed for this exact situation.
> 

I mixed up with_hash and in_bucket, you guys are right.  What threw me
off is that we store the hash as part of struct ecmp_hash_count
explicitly but also inside the hmap_node (we pass ecmp_hash_count->hash
to hmap_insert()):

struct ecmp_hash_count {
    struct hmap_node hmap_node;  <<< implicit here
    uint32_t hash;               <<< explicit here
    size_t n;
};

The two values are equal, should we get rid of the outer one to avoid
confusion?

The same stands for:

struct ecmp_id_slot {
    struct hmap_node hmap_node;
    uint16_t id;                <<< id is actually hmap_node.hash
    ...
};

> So I do believe it's indeed redundant. But anyways, it's such a minor
> thing that it probably doesn't really matter. But if we want it to be as
> clean as possible we could get rid of it.
> 

Maybe a small cleanup would be nice if one of you has time for it.

> Thanks,
> Rosemarie O'Riorden
> 

Thanks,
Dumitru

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to