~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to proxy/logging/LogCollationHostSM.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
//-------------------------------------------------------------------------
 
25
// include files
 
26
//-------------------------------------------------------------------------
 
27
 
 
28
#include "ink_config.h"
 
29
 
 
30
#include <stdio.h>
 
31
#include <stdlib.h>
 
32
#include <limits.h>
 
33
#include <string.h>
 
34
#include <sys/types.h>
 
35
 
 
36
#include "P_EventSystem.h"
 
37
#include "P_Net.h"
 
38
 
 
39
#include "LogUtils.h"
 
40
#include "LogSock.h"
 
41
#include "LogField.h"
 
42
#include "LogFile.h"
 
43
#include "LogFormat.h"
 
44
#include "LogBuffer.h"
 
45
#include "LogHost.h"
 
46
#include "LogObject.h"
 
47
#include "LogConfig.h"
 
48
#include "Log.h"
 
49
 
 
50
#include "LogCollationHostSM.h"
 
51
 
 
52
//-------------------------------------------------------------------------
 
53
// statics
 
54
//-------------------------------------------------------------------------
 
55
 
 
56
int
 
57
  LogCollationHostSM::ID = 0;
 
58
 
 
59
//-------------------------------------------------------------------------
 
60
// LogCollationHostSM::LogCollationHostSM
 
61
//-------------------------------------------------------------------------
 
62
 
 
63
LogCollationHostSM::LogCollationHostSM(NetVConnection * client_vc):
 
64
Continuation(new_ProxyMutex()),
 
65
m_client_vc(client_vc),
 
66
m_client_vio(NULL),
 
67
m_client_buffer(NULL),
 
68
m_client_reader(NULL),
 
69
m_pending_event(NULL),
 
70
m_read_buffer(NULL), m_read_bytes_wanted(0), m_read_bytes_received(0), m_client_ip(0), m_client_port(0), m_id(ID++)
 
71
{
 
72
 
 
73
  Debug("log-coll", "[%d]host::constructor", m_id);
 
74
 
 
75
  ink_assert(m_client_vc != NULL);
 
76
 
 
77
  // get client info
 
78
  m_client_ip = m_client_vc->get_remote_ip();
 
79
  m_client_port = m_client_vc->get_remote_port();
 
80
  Note("[log-coll] client connected [%d.%d.%d.%d:%d]",
 
81
       ((unsigned char *) (&m_client_ip))[0],
 
82
       ((unsigned char *) (&m_client_ip))[1],
 
83
       ((unsigned char *) (&m_client_ip))[2], ((unsigned char *) (&m_client_ip))[3], m_client_port);
 
84
 
 
85
  SET_HANDLER((LogCollationHostSMHandler) & LogCollationHostSM::host_handler);
 
86
  host_init(LOG_COLL_EVENT_SWITCH, NULL);
 
87
 
 
88
}
 
89
 
 
90
//-------------------------------------------------------------------------
 
91
//-------------------------------------------------------------------------
 
92
//
 
93
// handlers
 
94
//
 
95
//-------------------------------------------------------------------------
 
96
//-------------------------------------------------------------------------
 
97
 
 
98
//-------------------------------------------------------------------------
 
99
// LogCollationHostSM::host_handler
 
100
//-------------------------------------------------------------------------
 
101
 
 
102
int
 
103
LogCollationHostSM::host_handler(int event, void *data)
 
104
{
 
105
 
 
106
  switch (m_host_state) {
 
107
  case LOG_COLL_HOST_AUTH:
 
108
    return host_auth(event, data);
 
109
  case LOG_COLL_HOST_DONE:
 
110
    return host_done(event, data);
 
111
  case LOG_COLL_HOST_INIT:
 
112
    return host_init(event, data);
 
113
  case LOG_COLL_HOST_RECV:
 
114
    return host_recv(event, data);
 
115
  default:
 
116
    ink_assert(!"unexpected state");
 
117
    return EVENT_CONT;
 
118
  }
 
119
 
 
120
}
 
121
 
 
122
//-------------------------------------------------------------------------
 
123
// LogCollationHostSM::read_handler
 
124
//-------------------------------------------------------------------------
 
125
 
 
126
int
 
127
LogCollationHostSM::read_handler(int event, void *data)
 
128
{
 
129
 
 
130
  switch (m_read_state) {
 
131
  case LOG_COLL_READ_BODY:
 
132
    return read_body(event, (VIO *) data);
 
133
  case LOG_COLL_READ_HDR:
 
134
    return read_hdr(event, (VIO *) data);
 
135
  default:
 
136
    ink_assert(!"unexpected state");
 
137
    return EVENT_CONT;
 
138
  }
 
139
 
 
140
}
 
141
 
 
142
//-------------------------------------------------------------------------
 
143
//-------------------------------------------------------------------------
 
144
//
 
145
// host states
 
146
//
 
147
//-------------------------------------------------------------------------
 
148
//-------------------------------------------------------------------------
 
149
 
 
150
//-------------------------------------------------------------------------
 
151
// LogCollationHostSM::host_auth
 
152
// next:  host_done || host_recv
 
153
//-------------------------------------------------------------------------
 
154
 
 
155
int
 
156
LogCollationHostSM::host_auth(int event, void *data)
 
157
{
 
158
  NOWARN_UNUSED(data);
 
159
  Debug("log-coll", "[%d]host::host_auth", m_id);
 
160
 
 
161
  switch (event) {
 
162
 
 
163
  case LOG_COLL_EVENT_SWITCH:
 
164
    Debug("log-coll", "[%d]host::host_auth - SWITCH", m_id);
 
165
    m_host_state = LOG_COLL_HOST_AUTH;
 
166
    return read_start();
 
167
 
 
168
  case LOG_COLL_EVENT_READ_COMPLETE:
 
169
    Debug("log-coll", "[%d]host::host_auth - READ_COMPLETE", m_id);
 
170
    {
 
171
      // compare authorization secrets
 
172
      ink_assert(m_read_buffer != NULL);
 
173
      int diff = strncmp(m_read_buffer, Log::config->collation_secret,
 
174
                         m_read_bytes_received);
 
175
      delete[]m_read_buffer;
 
176
      m_read_buffer = 0;
 
177
      if (!diff) {
 
178
        Debug("log-coll", "[%d]host::host_auth - authenticated!", m_id);
 
179
        return host_recv(LOG_COLL_EVENT_SWITCH, NULL);
 
180
      } else {
 
181
        Debug("log-coll", "[%d]host::host_auth - authenticated failed!", m_id);
 
182
        Note("[log-coll] authentication failed [%d.%d.%d.%d:%d]",
 
183
             ((unsigned char *) (&m_client_ip))[0],
 
184
             ((unsigned char *) (&m_client_ip))[1],
 
185
             ((unsigned char *) (&m_client_ip))[2], ((unsigned char *) (&m_client_ip))[3], m_client_port);
 
186
        return host_done(LOG_COLL_EVENT_SWITCH, NULL);
 
187
      }
 
188
 
 
189
    }
 
190
 
 
191
  case LOG_COLL_EVENT_ERROR:
 
192
    Debug("log-coll", "[%d]host::host_auth - ERROR", m_id);
 
193
    return host_done(LOG_COLL_EVENT_SWITCH, NULL);
 
194
 
 
195
  default:
 
196
    ink_assert(!"unexpected state");
 
197
    return EVENT_CONT;
 
198
 
 
199
  }
 
200
 
 
201
}
 
202
 
 
203
//-------------------------------------------------------------------------
 
204
// LogCollationHostSM::host_done
 
205
// next: none
 
206
//-------------------------------------------------------------------------
 
207
 
 
208
int
 
209
LogCollationHostSM::host_done(int event, void *data)
 
210
{
 
211
  NOWARN_UNUSED(event);
 
212
  NOWARN_UNUSED(data);
 
213
  Debug("log-coll", "[%d]host::host_done", m_id);
 
214
 
 
215
  // close connections
 
216
  if (m_client_vc) {
 
217
    Debug("log-coll", "[%d]host::host_done - disconnecting!", m_id);
 
218
    m_client_vc->do_io_close();
 
219
    m_client_vc = 0;
 
220
    Note("[log-coll] client disconnected [%d.%d.%d.%d:%d]",
 
221
         ((unsigned char *) (&m_client_ip))[0],
 
222
         ((unsigned char *) (&m_client_ip))[1],
 
223
         ((unsigned char *) (&m_client_ip))[2], ((unsigned char *) (&m_client_ip))[3], m_client_port);
 
224
  }
 
225
  // free memory
 
226
  if (m_client_buffer) {
 
227
    if (m_client_reader) {
 
228
      m_client_buffer->dealloc_reader(m_client_reader);
 
229
    }
 
230
    free_MIOBuffer(m_client_buffer);
 
231
  }
 
232
  // delete this state machine and return
 
233
  delete this;
 
234
  return EVENT_DONE;
 
235
 
 
236
}
 
237
 
 
238
//-------------------------------------------------------------------------
 
239
// LogCollationHostSM::host_init
 
240
// next: host_auth || host_done
 
241
//-------------------------------------------------------------------------
 
242
 
 
243
int
 
244
LogCollationHostSM::host_init(int event, void *data)
 
245
{
 
246
  NOWARN_UNUSED(data);
 
247
  Debug("log-coll", "[%d]host::host_init", m_id);
 
248
 
 
249
  switch (event) {
 
250
 
 
251
  case LOG_COLL_EVENT_SWITCH:
 
252
    m_host_state = LOG_COLL_HOST_INIT;
 
253
    m_pending_event = eventProcessor.schedule_imm(this);
 
254
    return EVENT_CONT;
 
255
 
 
256
  case EVENT_IMMEDIATE:
 
257
    // allocate memory
 
258
    m_client_buffer = new_MIOBuffer();
 
259
    ink_assert(m_client_buffer != NULL);
 
260
    m_client_reader = m_client_buffer->alloc_reader();
 
261
    ink_assert(m_client_reader != NULL);
 
262
    return host_auth(LOG_COLL_EVENT_SWITCH, NULL);
 
263
 
 
264
  default:
 
265
    ink_assert(!"unexpected state");
 
266
    return EVENT_DONE;
 
267
 
 
268
  }
 
269
 
 
270
}
 
271
 
 
272
//-------------------------------------------------------------------------
 
273
// LogCollationHostSM::host_recv
 
274
// next: host_done || host_recv
 
275
//-------------------------------------------------------------------------
 
276
 
 
277
int
 
278
LogCollationHostSM::host_recv(int event, void *data)
 
279
{
 
280
  NOWARN_UNUSED(data);
 
281
  Debug("log-coll", "[%d]host::host_recv", m_id);
 
282
 
 
283
  switch (event) {
 
284
 
 
285
  case LOG_COLL_EVENT_SWITCH:
 
286
    Debug("log-coll", "[%d]host::host_recv - SWITCH", m_id);
 
287
    m_host_state = LOG_COLL_HOST_RECV;
 
288
    return read_start();
 
289
 
 
290
  case LOG_COLL_EVENT_READ_COMPLETE:
 
291
    Debug("log-coll", "[%d]host::host_recv - READ_COMPLETE", m_id);
 
292
    {
 
293
      // grab the log_buffer
 
294
      LogBufferHeader *log_buffer_header;
 
295
      LogBuffer *log_buffer;
 
296
      LogFormat *log_format;
 
297
      LogObject *log_object;
 
298
      unsigned version;
 
299
 
 
300
      ink_assert(m_read_buffer != NULL);
 
301
      ink_assert(m_read_bytes_received >= (int64_t)sizeof(LogBufferHeader));
 
302
      log_buffer_header = (LogBufferHeader *) m_read_buffer;
 
303
 
 
304
      // convert the buffer we just received to host order
 
305
      LogBuffer::convert_to_host_order(log_buffer_header);
 
306
 
 
307
      version = log_buffer_header->version;
 
308
      if (version != LOG_SEGMENT_VERSION) {
 
309
        Note("[log-coll] invalid LogBuffer received; invalid version - "
 
310
             "buffer = %u, current = %u", version, LOG_SEGMENT_VERSION);
 
311
        delete[]m_read_buffer;
 
312
 
 
313
      } else {
 
314
        log_object = Log::match_logobject(log_buffer_header);
 
315
        if (!log_object) {
 
316
          Note("[log-coll] LogObject not found with fieldlist id; " "writing LogBuffer to scrap file");
 
317
          log_object = Log::global_scrap_object;
 
318
        }
 
319
        log_format = log_object->m_format;
 
320
        Debug("log-coll", "[%d]host::host_recv - using format '%s'", m_id, log_format->name());
 
321
 
 
322
        // make a new LogBuffer (log_buffer_header plus subsequent
 
323
        // buffer already converted to host order) and add it to the
 
324
        // object's flush queue
 
325
        //
 
326
        log_buffer = NEW(new LogBuffer(log_object, log_buffer_header));
 
327
        log_object->add_to_flush_queue(log_buffer);
 
328
        ink_mutex_acquire(&Log::flush_mutex);
 
329
        Log::flush_counter++;
 
330
        ink_cond_signal(&Log::flush_cond);
 
331
        ink_mutex_release(&Log::flush_mutex);
 
332
      }
 
333
 
 
334
#if defined(LOG_BUFFER_TRACKING)
 
335
      Debug("log-buftrak", "[%d]host::host_recv - network read complete", log_buffer_header->id);
 
336
#endif // defined(LOG_BUFFER_TRACKING)
 
337
 
 
338
      // get ready for next read (memory may not be freed!!!)
 
339
      m_read_buffer = 0;
 
340
 
 
341
      return host_recv(LOG_COLL_EVENT_SWITCH, NULL);
 
342
 
 
343
    }
 
344
 
 
345
  case LOG_COLL_EVENT_ERROR:
 
346
    Debug("log-coll", "[%d]host::host_recv - ERROR", m_id);
 
347
    return host_done(LOG_COLL_EVENT_SWITCH, NULL);
 
348
 
 
349
  default:
 
350
    ink_assert(!"unexpected state");
 
351
    return EVENT_DONE;
 
352
 
 
353
  }
 
354
 
 
355
}
 
356
 
 
357
//-------------------------------------------------------------------------
 
358
//-------------------------------------------------------------------------
 
359
//
 
360
// read states
 
361
//
 
362
//-------------------------------------------------------------------------
 
363
//-------------------------------------------------------------------------
 
364
 
 
365
//-------------------------------------------------------------------------
 
366
// LogCollationHostSM::read_start
 
367
// next: read_hdr
 
368
//-------------------------------------------------------------------------
 
369
 
 
370
int
 
371
LogCollationHostSM::read_start()
 
372
{
 
373
 
 
374
  Debug("log-coll", "[%d]host::read_start", m_id);
 
375
 
 
376
  SET_HANDLER((LogCollationHostSMHandler) & LogCollationHostSM::read_handler);
 
377
  if (m_read_buffer) {
 
378
    ink_assert(!"m_read_buffer still points to something, doh!");
 
379
  }
 
380
  return read_hdr(LOG_COLL_EVENT_SWITCH, NULL);
 
381
 
 
382
}
 
383
 
 
384
//-------------------------------------------------------------------------
 
385
// LogCollationHostSM::read_hdr
 
386
// next: read_body || read_done
 
387
//-------------------------------------------------------------------------
 
388
 
 
389
int
 
390
LogCollationHostSM::read_hdr(int event, VIO * vio)
 
391
{
 
392
 
 
393
  Debug("log-coll", "[%d]host::read_hdr", m_id);
 
394
 
 
395
  switch (event) {
 
396
 
 
397
  case LOG_COLL_EVENT_SWITCH:
 
398
    Debug("log-coll", "[%d]host:read_hdr - SWITCH", m_id);
 
399
    m_read_state = LOG_COLL_READ_HDR;
 
400
 
 
401
    m_read_bytes_wanted = sizeof(NetMsgHeader);
 
402
    m_read_bytes_received = 0;
 
403
    m_read_buffer = (char *) &m_net_msg_header;
 
404
    ink_assert(m_client_vc != NULL);
 
405
    Debug("log-coll", "[%d]host:read_hdr - do_io_read(%d)", m_id, m_read_bytes_wanted);
 
406
    m_client_vio = m_client_vc->do_io_read(this, m_read_bytes_wanted, m_client_buffer);
 
407
    ink_assert(m_client_vio != NULL);
 
408
    return EVENT_CONT;
 
409
 
 
410
  case VC_EVENT_IMMEDIATE:
 
411
    Debug("log-coll", "[%d]host::read_hdr - IMMEDIATE", m_id);
 
412
    return EVENT_CONT;
 
413
 
 
414
  case VC_EVENT_READ_READY:
 
415
    Debug("log-coll", "[%d]host::read_hdr - READ_READY", m_id);
 
416
    read_partial(vio);
 
417
    return EVENT_CONT;
 
418
 
 
419
  case VC_EVENT_READ_COMPLETE:
 
420
    Debug("log-coll", "[%d]host::read_hdr - READ_COMPLETE", m_id);
 
421
    read_partial(vio);
 
422
    ink_assert(m_read_bytes_wanted == m_read_bytes_received);
 
423
    return read_body(LOG_COLL_EVENT_SWITCH, NULL);
 
424
 
 
425
  case VC_EVENT_EOS:
 
426
  case VC_EVENT_ERROR:
 
427
    Debug("log-coll", "[%d]host::read_hdr - EOS|ERROR", m_id);
 
428
    return read_done(LOG_COLL_EVENT_ERROR, NULL);
 
429
 
 
430
  default:
 
431
    Debug("log-coll", "[%d]host::read_hdr - default %d", m_id, event);
 
432
    return read_done(LOG_COLL_EVENT_ERROR, NULL);
 
433
 
 
434
  }
 
435
 
 
436
}
 
437
 
 
438
//-------------------------------------------------------------------------
 
439
// LogCollationHostSM::read_body
 
440
// next: read_body || read_done
 
441
//-------------------------------------------------------------------------
 
442
 
 
443
int
 
444
LogCollationHostSM::read_body(int event, VIO * vio)
 
445
{
 
446
 
 
447
  Debug("log-coll", "[%d]host::read_body", m_id);
 
448
 
 
449
  switch (event) {
 
450
 
 
451
  case LOG_COLL_EVENT_SWITCH:
 
452
    Debug("log-coll", "[%d]host:read_body - SWITCH", m_id);
 
453
    m_read_state = LOG_COLL_READ_BODY;
 
454
 
 
455
    m_read_bytes_wanted = ntohl(m_net_msg_header.htonl_size);
 
456
    ink_assert(m_read_bytes_wanted > 0);
 
457
    m_read_bytes_received = 0;
 
458
    m_read_buffer = new char[m_read_bytes_wanted];
 
459
    ink_assert(m_read_buffer != NULL);
 
460
    ink_assert(m_client_vc != NULL);
 
461
    Debug("log-coll", "[%d]host:read_body - do_io_read(%d)", m_id, m_read_bytes_wanted);
 
462
    m_client_vio = m_client_vc->do_io_read(this, m_read_bytes_wanted, m_client_buffer);
 
463
    ink_assert(m_client_vio != NULL);
 
464
    return EVENT_CONT;
 
465
 
 
466
  case VC_EVENT_IMMEDIATE:
 
467
    Debug("log-coll", "[%d]host::read_body - IMMEDIATE", m_id);
 
468
    return EVENT_CONT;
 
469
 
 
470
  case VC_EVENT_READ_READY:
 
471
    Debug("log-coll", "[%d]host::read_body - READ_READY", m_id);
 
472
    read_partial(vio);
 
473
    return EVENT_CONT;
 
474
 
 
475
  case VC_EVENT_READ_COMPLETE:
 
476
    Debug("log-coll", "[%d]host::read_body - READ_COMPLETE", m_id);
 
477
    read_partial(vio);
 
478
    ink_assert(m_read_bytes_wanted == m_read_bytes_received);
 
479
    return read_done(LOG_COLL_EVENT_READ_COMPLETE, NULL);
 
480
 
 
481
  case VC_EVENT_EOS:
 
482
  case VC_EVENT_ERROR:
 
483
    Debug("log-coll", "[%d]host::read_body - EOS|ERROR", m_id);
 
484
    return read_done(LOG_COLL_EVENT_ERROR, NULL);
 
485
 
 
486
  default:
 
487
    Debug("log-coll", "[%d]host::read_body - default %d", m_id, event);
 
488
    return read_done(LOG_COLL_EVENT_ERROR, NULL);
 
489
 
 
490
  }
 
491
 
 
492
}
 
493
 
 
494
//-------------------------------------------------------------------------
 
495
// LogCollationHostSM::read_done
 
496
// next: give control back to host state-machine
 
497
//-------------------------------------------------------------------------
 
498
 
 
499
int
 
500
LogCollationHostSM::read_done(int event, void *data)
 
501
{
 
502
  NOWARN_UNUSED(data);
 
503
  SET_HANDLER((LogCollationHostSMHandler) & LogCollationHostSM::host_handler);
 
504
  return host_handler(event, NULL);
 
505
 
 
506
}
 
507
 
 
508
//-------------------------------------------------------------------------
 
509
// LogCollationHostSM::read_partial
 
510
//-------------------------------------------------------------------------
 
511
 
 
512
void
 
513
LogCollationHostSM::read_partial(VIO * vio)
 
514
{
 
515
 
 
516
  // checks
 
517
  ink_assert(vio != NULL);
 
518
  ink_assert(vio->vc_server == m_client_vc);
 
519
  ink_assert(m_client_buffer != NULL);
 
520
  ink_assert(m_client_reader != NULL);
 
521
 
 
522
  // careful not to read more than we have memory for
 
523
  char *p = &(m_read_buffer[m_read_bytes_received]);
 
524
  int64_t bytes_wanted_now = m_read_bytes_wanted - m_read_bytes_received;
 
525
  int64_t bytes_received_now = m_client_reader->read(p, bytes_wanted_now);
 
526
 
 
527
  m_read_bytes_received += bytes_received_now;
 
528
 
 
529
  // stats
 
530
  LOG_SUM_DYN_STAT(log_stat_bytes_received_from_network_stat, bytes_received_now);
 
531
 
 
532
}