Skip to content

Commit

Permalink
update tfw_sched_http according to the new http_match.c logic
Browse files Browse the repository at this point in the history
related to:
#5 - Load balancing
  • Loading branch information
vdmit11 committed Oct 13, 2014
1 parent c259d23 commit 6ab0144
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
6 changes: 4 additions & 2 deletions tempesta_fw/http_match.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ do_match(const TfwHttpReq *req, const TfwHttpMatchRule *rule)
tfw_http_match_fld_t field;
tfw_http_match_arg_t arg_type;

TFW_DBG("rule: %p, field: %#x, op: %#x, arg:%d:%d'%.*s'\n",
rule, rule->field, rule->op, rule->arg.type, rule->arg.len,
rule->arg.len, rule->arg.str);

BUG_ON(!req || !rule);
BUG_ON(rule->field < 0 || rule->field >= _TFW_HTTP_MATCH_F_COUNT);
BUG_ON(rule->op < 0 || rule->op >= _TFW_HTTP_MATCH_O_COUNT);
Expand Down Expand Up @@ -263,8 +267,6 @@ tfw_http_match_req(const TfwHttpReq *req, const TfwHttpMatchList *mlst)
TFW_DBG("Matching request: %p, list: %p\n", req, mlst);

list_for_each_entry(rule, &mlst->list, list) {
TFW_DBG("rule: %p, field: %#x, op: %#x\n",
rule, rule->field, rule->op);
if (do_match(req, rule))
return rule;
}
Expand Down
28 changes: 14 additions & 14 deletions tempesta_fw/sched/tfw_sched_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ MODULE_LICENSE("GPL");
#define MAX_SRV_PER_RULE 16
#define RULES_TEXT_BUF_SIZE 4096
#define IP_ADDR_TEXT_BUF_SIZE 32
#define RULE_ARG_BUF_SIZE 255

/**
* PtrSet is a generic set of pointers implemented by a plain array.
Expand Down Expand Up @@ -301,6 +302,7 @@ build_match_entry(TfwHttpMatchList *dst_mlst, const MatchEntry *src)
* member of @dst_mlst, so we can't touch @dst->rule.list here. */
dst->rule.field = src->rule.field;
dst->rule.op = src->rule.op;
dst->rule.arg.type = src->rule.arg.type;
dst->rule.arg.len = arg_len;
memcpy(dst->rule.arg.str, src->rule.arg.str, arg_len);

Expand All @@ -325,6 +327,7 @@ build_match_list(void)
TfwHttpMatchList *new_match_list = NULL;
MatchEntry *src_entry;


new_match_list = tfw_http_match_list_alloc();
if (!new_match_list) {
ERR("Can't allocate new match list\n");
Expand Down Expand Up @@ -612,9 +615,11 @@ parse_field(ParserState *s)
{
static const char *field_str_tbl[] = {
[TFW_HTTP_MATCH_F_NA] = STRINGIFY(TFW_HTTP_MATCH_F_NA),
[TFW_HTTP_MATCH_F_HDR_RAW] = "hdr_raw",
[TFW_HTTP_MATCH_F_HDR_CONN] = "hdr_conn",
[TFW_HTTP_MATCH_F_HDR_HOST] = "hdr_host",
[TFW_HTTP_MATCH_F_HOST] = "host",
[TFW_HTTP_MATCH_F_URI] = "uri",
[TFW_HTTP_MATCH_F_HDR_RAW] = "headers",
};
tfw_http_match_fld_t field;

Expand Down Expand Up @@ -660,24 +665,17 @@ static int
parse_arg(ParserState *s)
{
TfwHttpMatchArg *arg;
size_t old_size, new_size;

EXPECT(TOKEN_STR, s, return -1);
get_token(s);

old_size = TFW_HTTP_MATCH_CONT_SIZE(MatchEntry, 0);
new_size = TFW_HTTP_MATCH_CONT_SIZE(MatchEntry, s->len + 1);
s->entry = tfw_pool_realloc(s->mlst->pool, s->entry, old_size,
new_size);
if (!s->entry) {
PARSER_ERR(s, "can't reallocate match entry");
return -1;
}

arg = &s->entry->rule.arg;
BUG_ON(s->len >= RULE_ARG_BUF_SIZE);

arg->type = TFW_HTTP_MATCH_A_STR;
arg->len = s->len;
memcpy(arg->str, s->lexeme, arg->len);
arg->str[arg->len] = '\0';
memcpy(arg->str, s->lexeme, s->len);
arg->str[s->len] = '\0';

return 0;
}
Expand Down Expand Up @@ -748,7 +746,8 @@ parse_rules(ParserState *s)
int ret;

while (peek_token(s)) {
s->entry = tfw_http_match_entry_new(s->mlst, MatchEntry, rule, 0);
s->entry = tfw_http_match_entry_new(s->mlst, MatchEntry, rule,
RULE_ARG_BUF_SIZE);
if (!s->entry) {
PARSER_ERR(s, "can't allocate new match entry");
return -1;
Expand Down Expand Up @@ -790,6 +789,7 @@ run_parser(const char *input)
mlst = NULL;
}


return mlst;
}

Expand Down
22 changes: 22 additions & 0 deletions tempesta_fw/sched/tfw_sched_http_rules_example.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
uri = /foo/bar.html {
127.0.0.1:8080
127.0.0.2:8080
}

uri ^ /foo {
127.0.0.3:8080
}

hdr_raw ^ "X-Raw-Header: value" {
127.0.0.4:8080
}

hdr_conn = Keep-Alive {
127.0.0.5:8080 127.0.0.6:8080
}

uri ^ / {
127.0.0.1:8080
127.0.0.2:8080
127.0.0.3:8080
}

0 comments on commit 6ab0144

Please sign in to comment.