flow/hash: fix and cleanup key/flow_id getters

Bug: #6205.
pull/9224/head
Victor Julien 2 years ago
parent 62170d2fb9
commit 3e53d5eea4

@ -944,75 +944,52 @@ flow_removed:
return NULL; return NULL;
} }
static inline int FlowCompareKey(Flow *f, FlowKey *key) /** \internal
* \retval true if flow matches key
* \retval false if flow does not match key, or unsupported protocol
* \note only supports TCP & UDP
*/
static inline bool FlowCompareKey(Flow *f, FlowKey *key)
{ {
if ((f->proto != IPPROTO_TCP) && (f->proto != IPPROTO_UDP)) if ((f->proto != IPPROTO_TCP) && (f->proto != IPPROTO_UDP))
return 0; return false;
return CmpFlowKey(f, key); return CmpFlowKey(f, key);
} }
/** \brief Look for existing Flow using a flow id value /** \brief Look for existing Flow using a flow id value
* *
* Hash retrieval function for flows. Looks up the hash bucket containing the * Hash retrieval function for flows. Looks up the hash bucket containing the
* flow pointer. Then compares the packet with the found flow to see if it is * flow pointer. Then compares the flow_id with the found flow's flow_id to see
* the flow we need. If it isn't, walk the list until the right flow is found. * if it is the flow we need.
*
* *
* \param flow_id Flow ID of the flow to look for * \param flow_id Flow ID of the flow to look for
* \retval f *LOCKED* flow or NULL * \retval f *LOCKED* flow or NULL
*/ */
Flow *FlowGetExistingFlowFromFlowId(int64_t flow_id) Flow *FlowGetExistingFlowFromFlowId(int64_t flow_id)
{ {
uint32_t hash = flow_id & 0x0000FFFF; uint32_t hash = flow_id & 0x0000FFFF;
/* get our hash bucket and lock it */
FlowBucket *fb = &flow_hash[hash % flow_config.hash_size]; FlowBucket *fb = &flow_hash[hash % flow_config.hash_size];
FBLOCK_LOCK(fb); FBLOCK_LOCK(fb);
SCLogDebug("fb %p fb->head %p", fb, fb->head); SCLogDebug("fb %p fb->head %p", fb, fb->head);
/* return if the bucket don't have a flow */ for (Flow *f = fb->head; f != NULL; f = f->next) {
if (fb->head == NULL) { if (FlowGetId(f) == flow_id) {
FBLOCK_UNLOCK(fb); /* found our flow, lock & return */
return NULL; FLOWLOCK_WRLOCK(f);
} FBLOCK_UNLOCK(fb);
return f;
/* ok, we have a flow in the bucket. Let's find out if it is our flow */
Flow *f = fb->head;
/* see if this is the flow we are looking for */
if (FlowGetId(f) != flow_id) {
while (f) {
f = f->next;
if (f == NULL) {
FBLOCK_UNLOCK(fb);
return NULL;
}
if (FlowGetId(f) != flow_id) {
/* found our flow, lock & return */
FLOWLOCK_WRLOCK(f);
FBLOCK_UNLOCK(fb);
return f;
}
} }
} }
/* lock & return */
FLOWLOCK_WRLOCK(f);
FBLOCK_UNLOCK(fb); FBLOCK_UNLOCK(fb);
return f; return NULL;
} }
/** \brief Look for existing Flow using a FlowKey /** \brief Look for existing Flow using a FlowKey
* *
* Hash retrieval function for flows. Looks up the hash bucket containing the * Hash retrieval function for flows. Looks up the hash bucket containing the
* flow pointer. Then compares the packet with the found flow to see if it is * flow pointer. Then compares the key with the found flow to see if it is
* the flow we need. If it isn't, walk the list until the right flow is found. * the flow we need. If it isn't, walk the list until the right flow is found.
* *
*
* \param key Pointer to FlowKey build using flow to look for * \param key Pointer to FlowKey build using flow to look for
* \param hash Value of the flow hash * \param hash Value of the flow hash
* \retval f *LOCKED* flow or NULL * \retval f *LOCKED* flow or NULL
@ -1022,43 +999,20 @@ static Flow *FlowGetExistingFlowFromHash(FlowKey *key, const uint32_t hash)
/* get our hash bucket and lock it */ /* get our hash bucket and lock it */
FlowBucket *fb = &flow_hash[hash % flow_config.hash_size]; FlowBucket *fb = &flow_hash[hash % flow_config.hash_size];
FBLOCK_LOCK(fb); FBLOCK_LOCK(fb);
SCLogDebug("fb %p fb->head %p", fb, fb->head); SCLogDebug("fb %p fb->head %p", fb, fb->head);
/* return if the bucket don't have a flow */ for (Flow *f = fb->head; f != NULL; f = f->next) {
if (fb->head == NULL) { /* see if this is the flow we are looking for */
FBLOCK_UNLOCK(fb); if (FlowCompareKey(f, key)) {
return NULL; /* found our flow, lock & return */
} FLOWLOCK_WRLOCK(f);
FBLOCK_UNLOCK(fb);
/* ok, we have a flow in the bucket. Let's find out if it is our flow */ return f;
Flow *f = fb->head;
/* see if this is the flow we are looking for */
if (FlowCompareKey(f, key) == 0) {
while (f) {
f = f->next;
if (f == NULL) {
FBLOCK_UNLOCK(fb);
return NULL;
}
if (FlowCompareKey(f, key) != 0) {
/* found our flow, lock & return */
FLOWLOCK_WRLOCK(f);
FBLOCK_UNLOCK(fb);
return f;
}
} }
} }
/* lock & return */
FLOWLOCK_WRLOCK(f);
FBLOCK_UNLOCK(fb); FBLOCK_UNLOCK(fb);
return f; return NULL;
} }
/** \brief Get or create a Flow using a FlowKey /** \brief Get or create a Flow using a FlowKey

Loading…
Cancel
Save