flow: simplify hash lookup logic

Remove double compare paths in favor of a single unified path.
pull/5279/head
Victor Julien 6 years ago
parent 8b016cff4b
commit 7583a6c37c

@ -634,49 +634,17 @@ Flow *FlowGetFlowFromHash(ThreadVars *tv, DecodeThreadVars *dtv, const Packet *p
} }
/* ok, we have a flow in the bucket. Let's find out if it is our flow */ /* ok, we have a flow in the bucket. Let's find out if it is our flow */
f = fb->head;
/* see if this is the flow we are looking for */
if (FlowCompare(f, p) == 0) {
Flow *pf = NULL; /* previous flow */ Flow *pf = NULL; /* previous flow */
f = fb->head;
while (f) { do {
pf = f;
f = f->hnext;
if (f == NULL) {
f = pf->hnext = FlowGetNew(tv, dtv, p);
if (f == NULL) {
FBLOCK_UNLOCK(fb);
return NULL;
}
fb->tail = f;
/* flow is locked */
f->hprev = pf;
/* initialize and return */
FlowInit(f, p);
f->flow_hash = hash;
f->fb = fb;
FlowUpdateState(f, FLOW_STATE_NEW);
FlowReference(dest, f);
FBLOCK_UNLOCK(fb);
return f;
}
if (FlowCompare(f, p) != 0) { if (FlowCompare(f, p) != 0) {
/* we found our flow, lets put it on top of the /* we found our flow, lets put it on top of the
* hash list -- this rewards active flows */ * hash list -- this rewards active flows */
if (f->hprev) {
if (f->hnext) { if (f->hnext) {
f->hnext->hprev = f->hprev; f->hnext->hprev = f->hprev;
} }
if (f->hprev) {
f->hprev->hnext = f->hnext; f->hprev->hnext = f->hnext;
}
if (f == fb->tail) { if (f == fb->tail) {
fb->tail = f->hprev; fb->tail = f->hprev;
} }
@ -685,7 +653,7 @@ Flow *FlowGetFlowFromHash(ThreadVars *tv, DecodeThreadVars *dtv, const Packet *p
f->hprev = NULL; f->hprev = NULL;
fb->head->hprev = f; fb->head->hprev = f;
fb->head = f; fb->head = f;
}
/* found our flow, lock & return */ /* found our flow, lock & return */
FLOWLOCK_WRLOCK(f); FLOWLOCK_WRLOCK(f);
if (unlikely(TcpSessionPacketSsnReuse(p, f, f->protoctx) == 1)) { if (unlikely(TcpSessionPacketSsnReuse(p, f, f->protoctx) == 1)) {
@ -701,23 +669,35 @@ Flow *FlowGetFlowFromHash(ThreadVars *tv, DecodeThreadVars *dtv, const Packet *p
FBLOCK_UNLOCK(fb); FBLOCK_UNLOCK(fb);
return f; return f;
} }
} if (f->hnext == NULL) {
} pf = f;
f = pf->hnext = FlowGetNew(tv, dtv, p);
/* lock & return */
FLOWLOCK_WRLOCK(f);
if (unlikely(TcpSessionPacketSsnReuse(p, f, f->protoctx) == 1)) {
f = TcpReuseReplace(tv, dtv, fb, f, hash, p);
if (f == NULL) { if (f == NULL) {
FBLOCK_UNLOCK(fb); FBLOCK_UNLOCK(fb);
return NULL; return NULL;
} }
} fb->tail = f;
FlowReference(dest, f); /* flow is locked */
f->hprev = pf;
/* initialize and return */
FlowInit(f, p);
f->flow_hash = hash;
f->fb = fb;
FlowUpdateState(f, FLOW_STATE_NEW);
FlowReference(dest, f);
FBLOCK_UNLOCK(fb); FBLOCK_UNLOCK(fb);
return f; return f;
}
pf = f;
f = f->hnext;
} while (f != NULL);
/* should be unreachable */
BUG_ON(1);
return NULL;
} }
static inline int FlowCompareKey(Flow *f, FlowKey *key) static inline int FlowCompareKey(Flow *f, FlowKey *key)

Loading…
Cancel
Save