]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MEDIUM] session: add a counter on the cumulated number of sessions
authorWilly Tarreau <w@1wt.eu>
Fri, 18 Jun 2010 20:10:12 +0000 (22:10 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 10 Aug 2010 16:04:13 +0000 (18:04 +0200)
Sessions are like connections but they have been accepted by L4 rules
and really became sessions.

include/types/stick_table.h
src/session.c
src/stick_table.c

index fef759b7770ef610c47faaf6f5efc48439df911e..4cd18d3f7b05ee0a1db8842dcd3b513ba1ba56bb 100644 (file)
@@ -44,6 +44,7 @@ enum {
        STKTABLE_DT_SERVER_ID,    /* the server ID to use with this session if > 0 */
        STKTABLE_DT_CONN_CNT,     /* cumulated number of connections */
        STKTABLE_DT_CONN_CUR,     /* concurrent number of connections */
+       STKTABLE_DT_SESS_CNT,     /* cumulated number of sessions (accepted connections) */
        STKTABLE_DT_BYTES_IN_CNT, /* cumulated bytes count from client to servers */
        STKTABLE_DT_BYTES_OUT_CNT,/* cumulated bytes count from servers to client */
        STKTABLE_DATA_TYPES       /* Number of data types, must always be last */
@@ -54,6 +55,7 @@ union stktable_data {
        int server_id;
        unsigned int conn_cnt;
        unsigned int conn_cur;
+       unsigned int sess_cnt;
        unsigned long long bytes_in_cnt;
        unsigned long long bytes_out_cnt;
 };
index 9016a02e4d031fdc753f4cb59938b5d32d18afe8..ed854d981acf60a465d5b58d78c85b34836c6941 100644 (file)
@@ -134,6 +134,11 @@ int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
        if (p->feconn > p->counters.feconn_max)
                p->counters.feconn_max = p->feconn;
        proxy_inc_fe_sess_ctr(l, p);
+       if (s->tracked_counters) {
+               void *ptr = stktable_data_ptr(s->tracked_table, s->tracked_counters, STKTABLE_DT_SESS_CNT);
+               if (ptr)
+                       stktable_data_cast(ptr, sess_cnt)++;
+       }
 
        /* this part should be common with other protocols */
        s->si[0].fd        = cfd;
@@ -2208,6 +2213,54 @@ acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
        return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
 }
 
+/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
+static int
+acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
+{
+       test->flags = ACL_TEST_F_VOL_TEST;
+       test->i = 0;
+       if (ts != NULL) {
+               void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
+               if (!ptr)
+                       return 0; /* parameter not stored */
+               test->i = stktable_data_cast(ptr, sess_cnt);
+       }
+       return 1;
+}
+
+/* set test->i to the cumulated number of sessions from the session's tracked counters */
+static int
+acl_fetch_trk_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
+                      struct acl_expr *expr, struct acl_test *test)
+{
+       if (!l4->tracked_counters)
+               return 0;
+
+       return acl_fetch_sess_cnt(l4->tracked_table, test, l4->tracked_counters);
+}
+
+/* set test->i to the cumulated number of session from the session's source
+ * address in the table pointed to by expr.
+ */
+static int
+acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
+                      struct acl_expr *expr, struct acl_test *test)
+{
+       struct stktable_key *key;
+
+       key = tcpv4_src_to_stktable_key(l4);
+       if (!key)
+               return 0; /* only TCPv4 is supported right now */
+
+       if (expr->arg_len)
+               px = find_stktable(expr->arg.str);
+
+       if (!px)
+               return 0; /* table not found */
+
+       return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
+}
+
 /* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
 static int
 acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
@@ -2318,6 +2371,8 @@ static struct acl_kw_list acl_kws = {{ },{
        { "src_updt_conn_cnt",  acl_parse_int,   acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
        { "trk_conn_cur",       acl_parse_int,   acl_fetch_trk_conn_cur,      acl_match_int, ACL_USE_NOTHING },
        { "src_conn_cur",       acl_parse_int,   acl_fetch_src_conn_cur,      acl_match_int, ACL_USE_TCP4_VOLATILE },
+       { "trk_sess_cnt",       acl_parse_int,   acl_fetch_trk_sess_cnt,      acl_match_int, ACL_USE_NOTHING },
+       { "src_sess_cnt",       acl_parse_int,   acl_fetch_src_sess_cnt,      acl_match_int, ACL_USE_TCP4_VOLATILE },
        { "trk_kbytes_in",      acl_parse_int,   acl_fetch_trk_kbytes_in,     acl_match_int, ACL_USE_TCP4_VOLATILE },
        { "src_kbytes_in",      acl_parse_int,   acl_fetch_src_kbytes_in,     acl_match_int, ACL_USE_TCP4_VOLATILE },
        { "trk_kbytes_out",     acl_parse_int,   acl_fetch_trk_kbytes_out,    acl_match_int, ACL_USE_TCP4_VOLATILE },
index d701d09e809675e185de729d2c9a4d1338fc819c..e68cae35caf694f88c0f703f6a3ecacdb9abab65 100644 (file)
@@ -528,6 +528,7 @@ struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
        [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .data_length = stktable_data_size(server_id) },
        [STKTABLE_DT_CONN_CNT]  = { .name = "conn_cnt",  .data_length = stktable_data_size(conn_cnt)  },
        [STKTABLE_DT_CONN_CUR]  = { .name = "conn_cur",  .data_length = stktable_data_size(conn_cur)  },
+       [STKTABLE_DT_SESS_CNT]  = { .name = "sess_cnt",  .data_length = stktable_data_size(sess_cnt)  },
        [STKTABLE_DT_BYTES_IN_CNT]  = { .name = "bytes_in_cnt",  .data_length = stktable_data_size(bytes_in_cnt)  },
        [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .data_length = stktable_data_size(bytes_out_cnt) },
 };