Skip to content

Commit

Permalink
Rework the code while addressing the code review issues. (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
keshonok committed Jun 5, 2016
1 parent ba7acdc commit ee203a9
Show file tree
Hide file tree
Showing 11 changed files with 766 additions and 776 deletions.
2 changes: 1 addition & 1 deletion tempesta_fw/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ tempesta_fw-objs = \
sock_srv.o \
ss_skb.o \
stress.o \
tfw_tfwcfg.o \
str.o \
tls.o \
vhost.o \
work_queue.o

obj-m += log/ classifier/ stress/ sched/ t/
89 changes: 52 additions & 37 deletions tempesta_fw/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "tdb.h"

#include "tempesta_fw.h"
#include "tfw_tfwcfg.h"
#include "vhost.h"
#include "cache.h"
#include "http_msg.h"
#include "procfs.h"
Expand Down Expand Up @@ -192,24 +192,40 @@ tfw_cache_msg_cacheable(TfwHttpReq *req)
}

/*
* Match the given request against strings specified in "location",
* "cache_bypass", and "cache_fulfil" directives and according to
* match operators specified in the directives.
*
* Return the constant that identifies the action on a request.
* Currently the actions are "bypass", "fulfill", or "default".
* Find caching policy in specific vhost and location.
*/
tfw_stmt_t
tfw_cache_action(TfwHttpReq *req)
static int
tfw_cache_policy(TfwVhost *vhost, TfwLocation *loc, TfwStr *arg)
{
TfwCfgLocation *loc;
TfwCfgCacheMatch *cam;

loc = tfw_location_match(&req->uri_path);
cam = tfw_camatch_match(loc, &req->uri_path);
if (cam)
return cam->stmt;
return TFW_D_CACHE_DEFAULT;
TfwCaPolicy *capo;

/* Search locations in current vhost. */
if (loc && loc->capo_sz) {
if ((capo = tfw_capolicy_match(loc, arg)))
return capo->cmd;
}

/*
* Search default policies in current vhost.
* If there's none, then search global default policies.
*/
loc = vhost->loc_dflt;
if (loc && loc->capo_sz) {
if ((capo = tfw_capolicy_match(loc, arg)))
return capo->cmd;
} else {
TfwVhost *vhost_dflt = tfw_vhost_get_default();
if (vhost == vhost_dflt)
return TFW_D_CACHE_BYPASS;

loc = vhost_dflt->loc_dflt;
if (loc && loc->capo_sz) {
if ((capo = tfw_capolicy_match(loc, arg)))
return capo->cmd;
}
}

return TFW_D_CACHE_BYPASS;
}

/*
Expand All @@ -221,26 +237,25 @@ tfw_cache_action(TfwHttpReq *req)
* the resulting decision.
*/
static bool
tfw_cache_employ(TfwHttpReq *req, TfwHttpResp *resp)
tfw_cache_employ_req(TfwHttpReq *req)
{
/*
* Process requests according to "location", "cache_bypass",
* and "cache_fulfill" directives.
*/
if (!resp) {
tfw_stmt_t stmt = tfw_cache_action(req);
if ((stmt == TFW_D_CACHE_DEFAULT)
|| (stmt == TFW_D_CACHE_BYPASS))
{
req->cache_ctl.flags |= TFW_HTTP_CC_NO_STORE;
return false;
}
/* cache_fulfill - work as usual in cache mode. */
BUG_ON(stmt != TFW_D_CACHE_FULFILL);
} else {
if (req->cache_ctl.flags & TFW_HTTP_CC_NO_STORE)
return false;
int cmd = tfw_cache_policy(req->vhost, req->location, &req->uri_path);

if (cmd == TFW_D_CACHE_BYPASS) {
req->cache_ctl.flags |= TFW_HTTP_CC_CACHE_BYPASS;
return false;
}
/* cache_fulfill - work as usual in cache mode. */
BUG_ON(cmd != TFW_D_CACHE_FULFILL);

return true;
}

static bool
tfw_cache_employ_resp(TfwHttpReq *req, TfwHttpResp *resp)
{
if (req->cache_ctl.flags & TFW_HTTP_CC_CACHE_BYPASS)
return false;
return true;
}

Expand Down Expand Up @@ -626,7 +641,7 @@ tfw_cache_add(TfwHttpResp *resp, TfwHttpReq *req)

if (!cache_cfg.cache || !tfw_cache_msg_cacheable(req))
return true;
if (!tfw_cache_employ(req, resp))
if (!tfw_cache_employ_resp(req, resp))
return true;

key = tfw_http_req_key_calc(req);
Expand Down Expand Up @@ -668,7 +683,7 @@ tfw_cache_process(TfwHttpReq *req, TfwHttpResp *resp,

if (!cache_cfg.cache || !tfw_cache_msg_cacheable(req))
goto dont_cache;
if (!tfw_cache_employ(req, resp))
if (!resp && !tfw_cache_employ_req(req))
goto dont_cache;

cw.req = req;
Expand Down
13 changes: 13 additions & 0 deletions tempesta_fw/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,14 @@ tfw_http_req_cache_cb(TfwHttpReq *req, TfwHttpResp *resp)
tfw_srv_conn_release(srv_conn);
}

static int
tfw_http_req_set_context(TfwHttpReq *req)
{
req->vhost = tfw_vhost_match(&req->uri_path);
req->location = tfw_location_match(req->vhost, &req->uri_path);
return (!req->location);
}

/**
* @return zero on success and negative value otherwise.
* TODO enter the function depending on current GFSM state.
Expand Down Expand Up @@ -811,6 +819,10 @@ tfw_http_req_process(TfwConnection *conn, struct sk_buff *skb, unsigned int off)
return TFW_BLOCK;
}

/* Assign the right Vhost for this request. */
if (tfw_http_req_set_context((TfwHttpReq *)hmreq))
return TFW_BLOCK;

/*
* In HTTP 0.9 the server always closes the connection
* after sending the response.
Expand Down Expand Up @@ -868,6 +880,7 @@ tfw_http_req_process(TfwConnection *conn, struct sk_buff *skb, unsigned int off)
return TFW_BLOCK;
}
}

/*
* Complete HTTP message has been collected and processed
* with success. Mark the message as complete in @conn as
Expand Down
4 changes: 4 additions & 0 deletions tempesta_fw/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define __TFW_HTTP_H__

#include "connection.h"
#include "vhost.h"
#include "gfsm.h"
#include "msg.h"
#include "str.h"
Expand Down Expand Up @@ -103,6 +104,7 @@ enum {
#define TFW_HTTP_CC_PROXY_REV 0x040
#define TFW_HTTP_CC_PUBLIC 0x080
#define TFW_HTTP_CC_PRIVATE 0x100
#define TFW_HTTP_CC_CACHE_BYPASS 0x100000
typedef struct {
unsigned int flags;
unsigned int max_age;
Expand Down Expand Up @@ -255,6 +257,8 @@ typedef struct {
*/
typedef struct {
TFW_HTTP_MSG_COMMON;
TfwVhost *vhost;
TfwLocation *location;
TfwStr userinfo;
TfwStr host;
TfwStr uri_path;
Expand Down
2 changes: 2 additions & 0 deletions tempesta_fw/http_match.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ typedef enum {
_TFW_HTTP_MATCH_O_COUNT
} tfw_http_match_op_t;

typedef tfw_http_match_op_t tfw_match_t;

typedef enum {
TFW_HTTP_MATCH_A_NA = 0,
TFW_HTTP_MATCH_A_WILDCARD,
Expand Down
4 changes: 2 additions & 2 deletions tempesta_fw/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ tfw_init(void)

DO_INIT(cfg_if);
DO_INIT(procfs);
DO_INIT(tfwcfg);
DO_INIT(vhost);

DO_INIT(classifier);

Expand All @@ -91,7 +91,7 @@ tfw_init(void)
DO_INIT(sock_srv);
DO_INIT(sock_clnt);

DO_CFG_REG(tfwcfg);
DO_CFG_REG(vhost);
DO_CFG_REG(filter);
DO_CFG_REG(cache);
DO_CFG_REG(http_sticky);
Expand Down
2 changes: 1 addition & 1 deletion tempesta_fw/t/unit/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <linux/module.h>
#include "test.h"

#include "tfw_tfwcfg.c"
#include "vhost.c"

int test_fail_counter;
test_fixture_fn_t test_setup_fn;
Expand Down
Loading

0 comments on commit ee203a9

Please sign in to comment.