1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #pragma once
25 #include "P_Cache.h"
26 #include "tscore/MatcherUtils.h"
27 #include "tscore/HostLookup.h"
28 
29 #define CACHE_MEM_FREE_TIMEOUT HRTIME_SECONDS(1)
30 
31 struct Vol;
32 struct CacheVol;
33 
34 struct CacheHostResult;
35 struct Cache;
36 
37 struct CacheHostRecord {
38   int Init(CacheType typ);
39   int Init(matcher_line *line_info, CacheType typ);
40 
41   void UpdateMatch(CacheHostResult *r, char *rd);
42   void Print() const;
43 
~CacheHostRecordCacheHostRecord44   ~CacheHostRecord()
45   {
46     ats_free(vols);
47     ats_free(vol_hash_table);
48     ats_free(cp);
49   }
50 
51   CacheType type                 = CACHE_NONE_TYPE;
52   Vol **vols                     = nullptr;
53   int good_num_vols              = 0;
54   int num_vols                   = 0;
55   int num_initialized            = 0;
56   unsigned short *vol_hash_table = nullptr;
57   CacheVol **cp                  = nullptr;
58   int num_cachevols              = 0;
59 
CacheHostRecordCacheHostRecord60   CacheHostRecord() {}
61 };
62 
63 void build_vol_hash_table(CacheHostRecord *cp);
64 
65 struct CacheHostResult {
66   CacheHostRecord *record = nullptr;
67 
CacheHostResultCacheHostResult68   CacheHostResult() {}
69 };
70 
71 class CacheHostMatcher
72 {
73 public:
74   CacheHostMatcher(const char *name, CacheType typ);
75   ~CacheHostMatcher();
76 
77   void AllocateSpace(int num_entries);
78   void NewEntry(matcher_line *line_info);
79 
80   void Match(const char *rdata, int rlen, CacheHostResult *result) const;
81   void Print() const;
82 
83   int
getNumElements()84   getNumElements() const
85   {
86     return num_el;
87   }
88   CacheHostRecord *
getDataArray()89   getDataArray() const
90   {
91     return data_array;
92   }
93   HostLookup *
getHLookup()94   getHLookup() const
95   {
96     return host_lookup;
97   }
98 
99 private:
100   static void PrintFunc(void *opaque_data);
101   HostLookup *host_lookup;     // Data structure to do the lookups
102   CacheHostRecord *data_array; // array of all data items
103   int array_len;               // the length of the arrays
104   int num_el;                  // the number of items in the tree
105   CacheType type;
106 };
107 
108 class CacheHostTable
109 {
110 public:
111   // Parameter name must not be deallocated before this
112   //  object is
113   CacheHostTable(Cache *c, CacheType typ);
114   ~CacheHostTable();
115 
116   int BuildTable(const char *config_file_path);
117   int BuildTableFromString(const char *config_file_path, char *str);
118 
119   void Match(const char *rdata, int rlen, CacheHostResult *result) const;
120   void Print() const;
121 
122   int
getEntryCount()123   getEntryCount() const
124   {
125     return m_numEntries;
126   }
127   CacheHostMatcher *
getHostMatcher()128   getHostMatcher() const
129   {
130     return hostMatch;
131   }
132 
133   static int config_callback(const char *, RecDataT, RecData, void *);
134 
135   void
register_config_callback(CacheHostTable ** p)136   register_config_callback(CacheHostTable **p)
137   {
138     REC_RegisterConfigUpdateFunc("proxy.config.cache.hosting_filename", CacheHostTable::config_callback, (void *)p);
139   }
140 
141   CacheType type   = CACHE_HTTP_TYPE;
142   Cache *cache     = nullptr;
143   int m_numEntries = 0;
144   CacheHostRecord gen_host_rec;
145 
146 private:
147   CacheHostMatcher *hostMatch    = nullptr;
148   const matcher_tags config_tags = {"hostname", "domain", nullptr, nullptr, nullptr, nullptr, false};
149   const char *matcher_name       = "unknown"; // Used for Debug/Warning/Error messages
150 };
151 
152 struct CacheHostTableConfig;
153 typedef int (CacheHostTableConfig::*CacheHostTabHandler)(int, void *);
154 struct CacheHostTableConfig : public Continuation {
155   CacheHostTable **ppt;
CacheHostTableConfigCacheHostTableConfig156   CacheHostTableConfig(CacheHostTable **appt) : Continuation(nullptr), ppt(appt)
157   {
158     SET_HANDLER((CacheHostTabHandler)&CacheHostTableConfig::mainEvent);
159   }
160 
161   int
mainEventCacheHostTableConfig162   mainEvent(int event, Event *e)
163   {
164     (void)e;
165     (void)event;
166     CacheHostTable *t   = new CacheHostTable((*ppt)->cache, (*ppt)->type);
167     CacheHostTable *old = (CacheHostTable *)ink_atomic_swap(&t, *ppt);
168     new_Deleter(old, CACHE_MEM_FREE_TIMEOUT);
169     return EVENT_DONE;
170   }
171 };
172 
173 /* list of volumes in the volume.config file */
174 struct ConfigVol {
175   int number;
176   CacheType scheme;
177   off_t size;
178   bool in_percent;
179   bool ramcache_enabled;
180   int percent;
181   CacheVol *cachep;
182   LINK(ConfigVol, link);
183 };
184 
185 struct ConfigVolumes {
186   int num_volumes;
187   int num_http_volumes;
188   Queue<ConfigVol> cp_queue;
189   void read_config_file();
190   void BuildListFromString(char *config_file_path, char *file_buf);
191 
192   void
clear_allConfigVolumes193   clear_all()
194   {
195     // remove all the volumes from the queue
196     for (int i = 0; i < num_volumes; i++) {
197       cp_queue.pop();
198     }
199     // reset count variables
200     num_volumes      = 0;
201     num_http_volumes = 0;
202   }
203 };
204