Skip to content

Commit de48af3

Browse files
committed
storing client data in tdb and use their given time
1 parent 67247c0 commit de48af3

File tree

9 files changed

+304
-126
lines changed

9 files changed

+304
-126
lines changed

etc/tempesta_fw.conf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,3 +1129,39 @@
11291129
# Default:
11301130
# Health monitor is disabled.
11311131
#
1132+
1133+
# TAG: client_db
1134+
#
1135+
# Path to a client database file used as a storage for clients info.
1136+
# The same as cache_db.
1137+
#
1138+
# Default:
1139+
# client_db /opt/tempesta/db/client.tdb;
1140+
#
1141+
1142+
# TAG: client_tbl_size
1143+
#
1144+
# Size of client drop table.
1145+
#
1146+
# Syntax:
1147+
# client_tbl_size SIZE
1148+
#
1149+
# Default:
1150+
# client_tbl_size 16777216; # 16MB
1151+
#
1152+
1153+
# TAG: client_lifetime
1154+
#
1155+
# Client life time in seconds. Life time of client accounting data after last
1156+
# client connection was closed. The accounting data is used for Frang limits.
1157+
# Zero value means unlimited life time.
1158+
#
1159+
# Syntax:
1160+
# client_lifetime NUM;
1161+
#
1162+
# Example:
1163+
# client_lifetime 3600;
1164+
#
1165+
# Default:
1166+
# client_lifetime 0;
1167+
#

tempesta_db/core/htrie.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,3 +931,90 @@ tdb_htrie_exit(TdbHdr *dbh)
931931
{
932932
free_percpu(dbh->pcpu);
933933
}
934+
935+
static int
936+
tdb_bucket_walk(TdbHdr *dbh, TdbBucket *b, int (*fn)(void *))
937+
{
938+
TdbBucket *b_tmp;
939+
940+
read_lock_bh(&b->lock);
941+
942+
do {
943+
TdbRec *r = TDB_HTRIE_BCKT_1ST_REC(b);
944+
do {
945+
size_t rlen = sizeof(*r) + TDB_HTRIE_RBODYLEN(dbh, r);
946+
rlen = TDB_HTRIE_RALIGN(rlen);
947+
if ((char *)r + rlen - (char *)b > TDB_HTRIE_MINDREC
948+
&& r != TDB_HTRIE_BCKT_1ST_REC(b))
949+
break;
950+
if (tdb_live_rec(dbh, r)) {
951+
int res = fn(r->data);
952+
if (unlikely(res)) {
953+
read_unlock_bh(&b->lock);
954+
return res;
955+
}
956+
}
957+
r = (TdbRec *)((char *)r + rlen);
958+
} while ((char *)r + sizeof(*r) - (char *)b
959+
<= TDB_HTRIE_MINDREC);
960+
961+
b_tmp = TDB_HTRIE_BUCKET_NEXT(dbh, b);
962+
if (b_tmp)
963+
read_lock_bh(&b_tmp->lock);
964+
read_unlock_bh(&b->lock);
965+
b = b_tmp;
966+
} while (b);
967+
968+
return 0;
969+
}
970+
971+
static int
972+
tdb_node_visit(TdbHdr *dbh, TdbHtrieNode *node, int (*fn)(void *))
973+
{
974+
int bits;
975+
976+
for (bits = 0; bits < TDB_HTRIE_FANOUT; ++bits) {
977+
unsigned long o;
978+
979+
BUG_ON(TDB_HTRIE_RESOLVED(bits));
980+
981+
o = node->shifts[bits];
982+
983+
BUG_ON(o
984+
&& (TDB_DI2O(o & ~TDB_HTRIE_DBIT)
985+
< TDB_HDR_SZ(dbh) + sizeof(TdbExt)
986+
|| TDB_DI2O(o & ~TDB_HTRIE_DBIT)
987+
> dbh->dbsz));
988+
989+
if (o & TDB_HTRIE_DBIT) {
990+
TdbBucket *b;
991+
int res;
992+
993+
/* We're at a data pointer - resolve it. */
994+
o ^= TDB_HTRIE_DBIT;
995+
BUG_ON(!o);
996+
997+
b = (TdbBucket *)TDB_PTR(dbh, TDB_DI2O(o));
998+
999+
res = tdb_bucket_walk(dbh, b, fn);
1000+
if (unlikely(res)) {
1001+
read_unlock_bh(&b->lock);
1002+
return res;
1003+
}
1004+
} else {
1005+
if (o)
1006+
tdb_node_visit(dbh, TDB_PTR(dbh, TDB_II2O(o)),
1007+
fn);
1008+
}
1009+
}
1010+
1011+
return 0;
1012+
}
1013+
1014+
int
1015+
tdb_walk(TdbHdr *dbh, int (*fn)(void *))
1016+
{
1017+
TdbHtrieNode *node = TDB_HTRIE_ROOT(dbh);
1018+
1019+
return tdb_node_visit(dbh, node, fn);
1020+
}

tempesta_db/core/htrie.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,6 @@ TdbRec *tdb_htrie_next_rec(TdbHdr *dbh, TdbRec *r, TdbBucket **b,
140140
unsigned long key);
141141
TdbHdr *tdb_htrie_init(void *p, size_t db_size, unsigned int rec_len);
142142
void tdb_htrie_exit(TdbHdr *dbh);
143+
int tdb_walk(TdbHdr *dbh, int (*fn)(void *));
143144

144145
#endif /* __HTRIE_H__ */

tempesta_db/core/main.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ MODULE_DESCRIPTION("Tempesta DB");
3535
MODULE_VERSION(TDB_VERSION);
3636
MODULE_LICENSE("GPL");
3737

38+
static DEFINE_SPINLOCK(get_alloc_lock);
39+
3840
/**
3941
* Create TDB entry and copy @len contiguous bytes from @data to the entry.
4042
*/
@@ -222,6 +224,41 @@ tdb_get_db(const char *path, int node)
222224
return tdb_get(db);
223225
}
224226

227+
TdbRec *
228+
tdb_rec_get_alloc(TDB *db, unsigned long key, size_t *len,
229+
bool (*predicate)(TdbRec *, void *), bool *is_new, void *data)
230+
{
231+
TdbIter iter;
232+
TdbRec *r;
233+
234+
spin_lock(&get_alloc_lock);
235+
236+
*is_new = false;
237+
iter = tdb_rec_get(db, key);
238+
while (!TDB_ITER_BAD(iter)) {
239+
if ((*predicate)(iter.rec, data)) {
240+
spin_unlock(&get_alloc_lock);
241+
return iter.rec;
242+
}
243+
tdb_rec_next(db, &iter);
244+
}
245+
246+
*is_new = true;
247+
r = tdb_entry_alloc(db, key, len);
248+
249+
spin_unlock(&get_alloc_lock);
250+
251+
return r;
252+
}
253+
EXPORT_SYMBOL(tdb_rec_get_alloc);
254+
255+
int
256+
tdb_entry_walk(TDB *db, int (*fn)(void *))
257+
{
258+
return tdb_walk(db->hdr, fn);
259+
}
260+
EXPORT_SYMBOL(tdb_entry_walk);
261+
225262
/**
226263
* Open database file and @return its descriptor.
227264
* If the database is already opened, then returns the handler.

tempesta_db/core/tdb.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ TdbIter tdb_rec_get(TDB *db, unsigned long key);
173173
void tdb_rec_next(TDB *db, TdbIter *iter);
174174
void tdb_rec_put(void *rec);
175175
int tdb_info(char *buf, size_t len);
176+
TdbRec *tdb_rec_get_alloc(TDB *db, unsigned long key, size_t *len,
177+
bool (*predicate)(TdbRec *, void *), bool *is_new,
178+
void *data);
179+
int tdb_entry_walk(TDB *db, int (*fn)(void *));
176180

177181
/* Open/close database handler. */
178182
TDB *tdb_open(const char *path, size_t fsize, unsigned int rec_size, int node);

0 commit comments

Comments
 (0)