circuitmux.c 39.3 KB
Newer Older
Nick Mathewson's avatar
Nick Mathewson committed
1
/* * Copyright (c) 2012-2019, The Tor Project, Inc. */
2
3
4
5
6
/* See LICENSE for licensing information */

/**
 * \file circuitmux.c
 * \brief Circuit mux/cell selection abstraction
7
8
9
10
11
12
13
14
15
16
 *
 * A circuitmux is responsible for <b>MU</b>ltiple<b>X</b>ing all of the
 * circuits that are writing on a single channel. It keeps track of which of
 * these circuits has something to write (aka, "active" circuits), and which
 * one should write next.  A circuitmux corresponds 1:1 with a channel.
 *
 * There can be different implementations of the circuitmux's rules (which
 * decide which circuit is next to write).
 *
 * A circuitmux exposes three distinct
17
18
19
 * interfaces to other components:
 *
 * To channels, which each have a circuitmux_t, the supported operations
20
 * (invoked from relay.c) are:
21
 *
22
 *   circuitmux_get_first_active_circuit():
23
 *
24
 *     Pick one of the circuitmux's active circuits to send cells from.
25
 *
26
 *   circuitmux_notify_xmit_cells():
27
 *
28
 *     Notify the circuitmux that cells have been sent on a circuit.
29
30
31
 *
 * To circuits, the exposed operations are:
 *
32
 *   circuitmux_attach_circuit():
33
 *
34
35
36
 *     Attach a circuit to the circuitmux; this will allocate any policy-
 *     specific data wanted for this circuit and add it to the active
 *     circuits list if it has queued cells.
37
 *
38
 *   circuitmux_detach_circuit():
39
 *
40
41
42
43
44
45
46
47
 *     Detach a circuit from the circuitmux, freeing associated structures.
 *
 *   circuitmux_clear_num_cells():
 *
 *     Clear the circuitmux's cell counter for this circuit.
 *
 *   circuitmux_set_num_cells():
 *
48
49
50
 *     Set the circuitmux's cell counter for this circuit. One of
 *     circuitmuc_clear_num_cells() or circuitmux_set_num_cells() MUST be
 *     called when the number of cells queued on a circuit changes.
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 *
 * See circuitmux.h for the circuitmux_policy_t data structure, which contains
 * a table of function pointers implementing a circuit selection policy, and
 * circuitmux_ewma.c for an example of a circuitmux policy.  Circuitmux
 * policies can be manipulated with:
 *
 *   circuitmux_get_policy():
 *
 *     Return the current policy for a circuitmux_t, if any.
 *
 *   circuitmux_clear_policy():
 *
 *     Remove a policy installed on a circuitmux_t, freeing all associated
 *     data.  The circuitmux will revert to the built-in round-robin behavior.
 *
 *   circuitmux_set_policy():
 *
 *     Install a policy on a circuitmux_t; the appropriate callbacks will be
 *     made to attach all existing circuits to the new policy.
70
71
 **/

72
73
74
75
76
#include "core/or/or.h"
#include "core/or/channel.h"
#include "core/or/circuitlist.h"
#include "core/or/circuitmux.h"
#include "core/or/relay.h"
77

78
79
80
#include "core/or/cell_queue_st.h"
#include "core/or/destroy_cell_queue_st.h"
#include "core/or/or_circuit_st.h"
81

82
83
#include "lib/crypt_ops/crypto_util.h"

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Private typedefs for circuitmux.c
 */

/*
 * Map of muxinfos for circuitmux_t to use; struct is defined below (name
 * of struct must match HT_HEAD line).
 */
typedef struct chanid_circid_muxinfo_map chanid_circid_muxinfo_map_t;

/*
 * Hash table entry (yeah, calling it chanid_circid_muxinfo_s seems to
 * break the hash table code).
 */
typedef struct chanid_circid_muxinfo_t chanid_circid_muxinfo_t;

/*
 * Anything the mux wants to store per-circuit in the map; right now just
 * a count of queued cells.
 */

typedef struct circuit_muxinfo_s circuit_muxinfo_t;

/*
 * Structures for circuitmux.c
109
110
111
 */

struct circuitmux_s {
112
113
114
  /* Keep count of attached, active circuits */
  unsigned int n_circuits, n_active_circuits;

115
116
117
  /* Total number of queued cells on all circuits */
  unsigned int n_cells;

118
119
120
121
122
  /*
   * Map from (channel ID, circuit ID) pairs to circuit_muxinfo_t
   */
  chanid_circid_muxinfo_map_t *chanid_circid_map;

123
  /** List of queued destroy cells */
124
  destroy_cell_queue_t destroy_cell_queue;
125
126
127
128
129
130
131
132
133
  /** Boolean: True iff the last cell to circuitmux_get_first_active_circuit
   * returned the destroy queue. Used to force alternation between
   * destroy/non-destroy cells.
   *
   * XXXX There is no reason to think that alternating is a particularly good
   * approach -- it's just designed to prevent destroys from starving other
   * cells completely.
   */
  unsigned int last_cell_was_destroy : 1;
134
135
136
137
  /** Destroy counter: increment this when a destroy gets queued, decrement
   * when we unqueue it, so we can test to make sure they don't starve.
   */
  int64_t destroy_ctr;
138

139
  /*
140
141
142
   * Circuitmux policy; if this is non-NULL, it can override the built-
   * in round-robin active circuits behavior.  This is how EWMA works in
   * the new circuitmux_t world.
143
   */
144
  const circuitmux_policy_t *policy;
145

146
147
  /* Policy-specific data */
  circuitmux_policy_data_t *policy_data;
148
149
150
151
};

/*
 * This struct holds whatever we want to store per attached circuit on a
152
 * circuitmux_t; right now, just the count of queued cells and the direction.
153
154
155
 */

struct circuit_muxinfo_s {
156
  /* Count of cells on this circuit at last update */
157
  unsigned int cell_count;
158
159
  /* Direction of flow */
  cell_direction_t direction;
160
161
  /* Policy-specific data */
  circuitmux_policy_circ_data_t *policy_data;
162
163
  /* Mark bit for consistency checker */
  unsigned int mark:1;
164
165
166
167
168
169
170
171
172
173
174
175
};

/*
 * A map from channel ID and circuit ID to a circuit_muxinfo_t for that
 * circuit.
 */

struct chanid_circid_muxinfo_t {
  HT_ENTRY(chanid_circid_muxinfo_t) node;
  uint64_t chan_id;
  circid_t circ_id;
  circuit_muxinfo_t muxinfo;
176
177
};

178
179
180
181
/*
 * Static function declarations
 */

182
static inline int
183
184
chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
                         chanid_circid_muxinfo_t *b);
185
static inline unsigned int
186
chanid_circid_entry_hash(chanid_circid_muxinfo_t *a);
187
188
static chanid_circid_muxinfo_t *
circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ);
189
static void
190
circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ);
191
static void
192
circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ);
193

194
195
196
197
198
/* Static global variables */

/** Count the destroy balance to debug destroy queue logic */
static int64_t global_destroy_ctr = 0;

199
200
201
202
203
204
205
206
/* Function definitions */

/**
 * Helper for chanid_circid_cell_count_map_t hash table: compare the channel
 * ID and circuit ID for a and b, and return less than, equal to, or greater
 * than zero appropriately.
 */

207
static inline int
208
209
210
211
212
213
214
215
216
217
chanid_circid_entries_eq(chanid_circid_muxinfo_t *a,
                         chanid_circid_muxinfo_t *b)
{
    return a->chan_id == b->chan_id && a->circ_id == b->circ_id;
}

/**
 * Helper: return a hash based on circuit ID and channel ID in a.
 */

218
static inline unsigned int
219
220
221
222
223
224
225
226
227
228
229
230
chanid_circid_entry_hash(chanid_circid_muxinfo_t *a)
{
    return (((unsigned int)(a->circ_id) << 8) ^
            ((unsigned int)((a->chan_id >> 32) & 0xffffffff)) ^
            ((unsigned int)(a->chan_id & 0xffffffff)));
}

/* Declare the struct chanid_circid_muxinfo_map type */
HT_HEAD(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t);

/* Emit a bunch of hash table stuff */
HT_PROTOTYPE(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
231
             chanid_circid_entry_hash, chanid_circid_entries_eq)
232
233
234
HT_GENERATE2(chanid_circid_muxinfo_map, chanid_circid_muxinfo_t, node,
             chanid_circid_entry_hash, chanid_circid_entries_eq, 0.6,
             tor_reallocarray_, tor_free_)
235

236
237
238
239
/*
 * Circuitmux alloc/free functions
 */

240
241
242
243
244
245
246
247
248
249
250
251
/**
 * Allocate a new circuitmux_t
 */

circuitmux_t *
circuitmux_alloc(void)
{
  circuitmux_t *rv = NULL;

  rv = tor_malloc_zero(sizeof(*rv));
  rv->chanid_circid_map = tor_malloc_zero(sizeof(*( rv->chanid_circid_map)));
  HT_INIT(chanid_circid_muxinfo_map, rv->chanid_circid_map);
252
  destroy_cell_queue_init(&rv->destroy_cell_queue);
253
254
255
256

  return rv;
}

257
258
/**
 * Detach all circuits from a circuitmux (use before circuitmux_free())
259
260
261
 *
 * If <b>detached_out</b> is non-NULL, add every detached circuit_t to
 * detached_out.
262
263
264
 */

void
265
circuitmux_detach_all_circuits(circuitmux_t *cmux, smartlist_t *detached_out)
266
267
268
269
270
271
272
273
274
275
{
  chanid_circid_muxinfo_t **i = NULL, *to_remove;
  channel_t *chan = NULL;
  circuit_t *circ = NULL;

  tor_assert(cmux);

  i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  while (i) {
    to_remove = *i;
276
277
278
279
280

    if (! to_remove) {
      log_warn(LD_BUG, "Somehow, an HT iterator gave us a NULL pointer.");
      break;
    } else {
281
282
283
      /* Find a channel and circuit */
      chan = channel_find_by_global_id(to_remove->chan_id);
      if (chan) {
284
285
286
        circ =
          circuit_get_by_circid_channel_even_if_marked(to_remove->circ_id,
                                                       chan);
287
288
289
        if (circ) {
          /* Clear the circuit's mux for this direction */
          if (to_remove->muxinfo.direction == CELL_DIRECTION_OUT) {
290
291
292
293
            /*
             * Update active_circuits et al.; this does policy notifies, so
             * comes before freeing policy data
             */
294
295

            if (to_remove->muxinfo.cell_count > 0) {
296
              circuitmux_make_circuit_inactive(cmux, circ);
297
298
            }

299
300
            if (detached_out)
              smartlist_add(detached_out, circ);
301
          } else if (circ->magic == OR_CIRCUIT_MAGIC) {
302
303
304
305
            /*
             * Update active_circuits et al.; this does policy notifies, so
             * comes before freeing policy data
             */
306

307
            if (to_remove->muxinfo.cell_count > 0) {
308
              circuitmux_make_circuit_inactive(cmux, circ);
309
310
            }

311
312
            if (detached_out)
              smartlist_add(detached_out, circ);
313
314
315
          } else {
            /* Complain and move on */
            log_warn(LD_CIRC,
316
                     "Circuit %u/channel %"PRIu64 " had direction == "
317
                     "CELL_DIRECTION_IN, but isn't an or_circuit_t",
318
                     (unsigned)to_remove->circ_id,
319
                     (to_remove->chan_id));
320
          }
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

          /* Free policy-specific data if we have it */
          if (to_remove->muxinfo.policy_data) {
            /*
             * If we have policy data, assert that we have the means to
             * free it
             */
            tor_assert(cmux->policy);
            tor_assert(cmux->policy->free_circ_data);
            /* Call free_circ_data() */
            cmux->policy->free_circ_data(cmux,
                                         cmux->policy_data,
                                         circ,
                                         to_remove->muxinfo.policy_data);
            to_remove->muxinfo.policy_data = NULL;
          }
337
338
339
        } else {
          /* Complain and move on */
          log_warn(LD_CIRC,
340
                   "Couldn't find circuit %u (for channel %"PRIu64 ")",
341
                   (unsigned)to_remove->circ_id,
342
                   (to_remove->chan_id));
343
344
345
346
        }
      } else {
        /* Complain and move on */
        log_warn(LD_CIRC,
347
348
                 "Couldn't find channel %"PRIu64 " (for circuit id %u)",
                 (to_remove->chan_id),
349
                 (unsigned)to_remove->circ_id);
350
351
      }

352
353
      /* Assert that we don't have un-freed policy data for this circuit */
      tor_assert(to_remove->muxinfo.policy_data == NULL);
354
    }
355
356
357
358
359

    i = HT_NEXT_RMV(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);

    /* Free it */
    tor_free(to_remove);
360
361
362
363
364
365
366
  }

  cmux->n_circuits = 0;
  cmux->n_active_circuits = 0;
  cmux->n_cells = 0;
}

367
368
369
370
371
372
373
374
375
376
/** Reclaim all circuit IDs currently marked as unusable on <b>chan</b> because
 * of pending destroy cells in <b>cmux</b>.
 *
 * This function must be called AFTER circuits are unlinked from the (channel,
 * circuid-id) map with circuit_unlink_all_from_channel(), but before calling
 * circuitmux_free().
 */
void
circuitmux_mark_destroyed_circids_usable(circuitmux_t *cmux, channel_t *chan)
{
377
  destroy_cell_t *cell;
378
  TOR_SIMPLEQ_FOREACH(cell, &cmux->destroy_cell_queue.head, next) {
379
    channel_mark_circid_usable(chan, cell->circid);
380
381
382
  }
}

383
384
385
386
387
388
/**
 * Free a circuitmux_t; the circuits must be detached first with
 * circuitmux_detach_all_circuits().
 */

void
389
circuitmux_free_(circuitmux_t *cmux)
390
391
392
393
394
395
{
  if (!cmux) return;

  tor_assert(cmux->n_circuits == 0);
  tor_assert(cmux->n_active_circuits == 0);

396
397
398
399
400
401
402
  /*
   * Free policy-specific data if we have any; we don't
   * need to do circuitmux_set_policy(cmux, NULL) to cover
   * the circuits because they would have been handled in
   * circuitmux_detach_all_circuits() before this was
   * called.
   */
403
404
405
406
407
408
  if (cmux->policy && cmux->policy->free_cmux_data) {
    if (cmux->policy_data) {
      cmux->policy->free_cmux_data(cmux, cmux->policy_data);
      cmux->policy_data = NULL;
    }
  } else tor_assert(cmux->policy_data == NULL);
409
410
411
412
413
414

  if (cmux->chanid_circid_map) {
    HT_CLEAR(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
    tor_free(cmux->chanid_circid_map);
  }

415
416
417
418
419
420
421
422
423
  /*
   * We're throwing away some destroys; log the counter and
   * adjust the global counter by the queue size.
   */
  if (cmux->destroy_cell_queue.n > 0) {
    cmux->destroy_ctr -= cmux->destroy_cell_queue.n;
    global_destroy_ctr -= cmux->destroy_cell_queue.n;
    log_debug(LD_CIRC,
              "Freeing cmux at %p with %u queued destroys; the last cmux "
424
              "destroy balance was %"PRId64", global is %"PRId64,
425
              cmux, cmux->destroy_cell_queue.n,
426
427
              (cmux->destroy_ctr),
              (global_destroy_ctr));
428
429
430
  } else {
    log_debug(LD_CIRC,
              "Freeing cmux at %p with no queued destroys, the cmux destroy "
431
              "balance was %"PRId64", global is %"PRId64,
432
              cmux,
433
434
              (cmux->destroy_ctr),
              (global_destroy_ctr));
435
436
  }

437
  destroy_cell_queue_clear(&cmux->destroy_cell_queue);
438

439
440
441
  tor_free(cmux);
}

442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Circuitmux policy control functions
 */

/**
 * Remove any policy installed on cmux; all policy data will be freed and
 * cmux behavior will revert to the built-in round-robin active_circuits
 * mechanism.
 */

void
circuitmux_clear_policy(circuitmux_t *cmux)
{
  tor_assert(cmux);

  /* Internally, this is just setting policy to NULL */
458
  circuitmux_set_policy(cmux, NULL);
459
460
461
462
463
464
}

/**
 * Return the policy currently installed on a circuitmux_t
 */

465
466
MOCK_IMPL(const circuitmux_policy_t *,
circuitmux_get_policy, (circuitmux_t *cmux))
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
{
  tor_assert(cmux);

  return cmux->policy;
}

/**
 * Set policy; allocate for new policy, detach all circuits from old policy
 * if any, attach them to new policy, and free old policy data.
 */

void
circuitmux_set_policy(circuitmux_t *cmux,
                      const circuitmux_policy_t *pol)
{
  const circuitmux_policy_t *old_pol = NULL, *new_pol = NULL;
  circuitmux_policy_data_t *old_pol_data = NULL, *new_pol_data = NULL;
  chanid_circid_muxinfo_t **i = NULL;
  channel_t *chan = NULL;
  uint64_t last_chan_id_searched = 0;
  circuit_t *circ = NULL;

  tor_assert(cmux);

  /* Set up variables */
  old_pol = cmux->policy;
  old_pol_data = cmux->policy_data;
  new_pol = pol;

  /* Check if this is the trivial case */
  if (old_pol == new_pol) return;

  /* Allocate data for new policy, if any */
  if (new_pol && new_pol->alloc_cmux_data) {
    /*
     * If alloc_cmux_data is not null, then we expect to get some policy
     * data.  Assert that we also have free_cmux_data so we can free it
     * when the time comes, and allocate it.
     */
    tor_assert(new_pol->free_cmux_data);
    new_pol_data = new_pol->alloc_cmux_data(cmux);
    tor_assert(new_pol_data);
  }

  /* Install new policy and new policy data on cmux */
  cmux->policy = new_pol;
  cmux->policy_data = new_pol_data;

  /* Iterate over all circuits, attaching/detaching each one */
  i = HT_START(chanid_circid_muxinfo_map, cmux->chanid_circid_map);
  while (i) {
    /* Assert that this entry isn't NULL */
    tor_assert(*i);

    /*
     * Get the channel; since normal case is all circuits on the mux share a
     * channel, we cache last_chan_id_searched
     */
    if (!chan || last_chan_id_searched != (*i)->chan_id) {
      chan = channel_find_by_global_id((*i)->chan_id);
      last_chan_id_searched = (*i)->chan_id;
    }
    tor_assert(chan);

    /* Get the circuit */
532
    circ = circuit_get_by_circid_channel_even_if_marked((*i)->circ_id, chan);
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
    tor_assert(circ);

    /* Need to tell old policy it becomes inactive (i.e., it is active) ? */
    if (old_pol && old_pol->notify_circ_inactive &&
        (*i)->muxinfo.cell_count > 0) {
      old_pol->notify_circ_inactive(cmux, old_pol_data, circ,
                                    (*i)->muxinfo.policy_data);
    }

    /* Need to free old policy data? */
    if ((*i)->muxinfo.policy_data) {
      /* Assert that we have the means to free it if we have policy data */
      tor_assert(old_pol);
      tor_assert(old_pol->free_circ_data);
      /* Free it */
      old_pol->free_circ_data(cmux, old_pol_data, circ,
                             (*i)->muxinfo.policy_data);
      (*i)->muxinfo.policy_data = NULL;
    }

    /* Need to allocate new policy data? */
    if (new_pol && new_pol->alloc_circ_data) {
      /*
       * If alloc_circ_data is not null, we expect to get some per-circuit
       * policy data.  Assert that we also have free_circ_data so we can
       * free it when the time comes, and allocate it.
       */
      tor_assert(new_pol->free_circ_data);
      (*i)->muxinfo.policy_data =
        new_pol->alloc_circ_data(cmux, new_pol_data, circ,
                                 (*i)->muxinfo.direction,
                                 (*i)->muxinfo.cell_count);
    }

    /* Need to make active on new policy? */
    if (new_pol && new_pol->notify_circ_active &&
        (*i)->muxinfo.cell_count > 0) {
      new_pol->notify_circ_active(cmux, new_pol_data, circ,
                                  (*i)->muxinfo.policy_data);
    }

    /* Advance to next circuit map entry */
    i = HT_NEXT(chanid_circid_muxinfo_map, cmux->chanid_circid_map, i);
  }

  /* Free data for old policy, if any */
  if (old_pol_data) {
    /*
     * If we had old policy data, we should have an old policy and a free
     * function for it.
     */
    tor_assert(old_pol);
    tor_assert(old_pol->free_cmux_data);
    old_pol->free_cmux_data(cmux, old_pol_data);
    old_pol_data = NULL;
  }
}

591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
 * Circuitmux/circuit attachment status inquiry functions
 */

/**
 * Query the direction of an attached circuit
 */

cell_direction_t
circuitmux_attached_circuit_direction(circuitmux_t *cmux, circuit_t *circ)
{
  chanid_circid_muxinfo_t *hashent = NULL;

  /* Try to find a map entry */
  hashent = circuitmux_find_map_entry(cmux, circ);

  /*
   * This function should only be called on attached circuits; assert that
   * we had a map entry.
   */
  tor_assert(hashent);

  /* Return the direction from the map entry */
  return hashent->muxinfo.direction;
}

/**
 * Find an entry in the cmux's map for this circuit or return NULL if there
 * is none.
 */

static chanid_circid_muxinfo_t *
circuitmux_find_map_entry(circuitmux_t *cmux, circuit_t *circ)
{
  chanid_circid_muxinfo_t search, *hashent = NULL;

  /* Sanity-check parameters */
  tor_assert(cmux);
  tor_assert(cmux->chanid_circid_map);
  tor_assert(circ);

632
633
634
635
636
  /* Check if we have n_chan */
  if (circ->n_chan) {
    /* Okay, let's see if it's attached for n_chan/n_circ_id */
    search.chan_id = circ->n_chan->global_identifier;
    search.circ_id = circ->n_circ_id;
637

638
639
640
641
    /* Query */
    hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
                      &search);
  }
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687

  /* Found something? */
  if (hashent) {
    /*
     * Assert that the direction makes sense for a hashent we found by
     * n_chan/n_circ_id before we return it.
     */
    tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_OUT);
  } else {
    /* Not there, have we got a p_chan/p_circ_id to try? */
    if (circ->magic == OR_CIRCUIT_MAGIC) {
      search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
      /* Check for p_chan */
      if (TO_OR_CIRCUIT(circ)->p_chan) {
        search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
        /* Okay, search for that */
        hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
                          &search);
        /* Find anything? */
        if (hashent) {
          /* Assert that the direction makes sense before we return it */
          tor_assert(hashent->muxinfo.direction == CELL_DIRECTION_IN);
        }
      }
    }
  }

  /* Okay, hashent is it if it was there */
  return hashent;
}

/**
 * Query whether a circuit is attached to a circuitmux
 */

int
circuitmux_is_circuit_attached(circuitmux_t *cmux, circuit_t *circ)
{
  chanid_circid_muxinfo_t *hashent = NULL;

  /* Look if it's in the circuit map */
  hashent = circuitmux_find_map_entry(cmux, circ);

  return (hashent != NULL);
}

688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/**
 * Query whether a circuit is active on a circuitmux
 */

int
circuitmux_is_circuit_active(circuitmux_t *cmux, circuit_t *circ)
{
  chanid_circid_muxinfo_t *hashent = NULL;
  int is_active = 0;

  tor_assert(cmux);
  tor_assert(circ);

  /* Look if it's in the circuit map */
  hashent = circuitmux_find_map_entry(cmux, circ);
  if (hashent) {
    /* Check the number of cells on this circuit */
    is_active = (hashent->muxinfo.cell_count > 0);
  }
  /* else not attached, so not active */

  return is_active;
}

/**
 * Query number of available cells for a circuit on a circuitmux
 */

unsigned int
circuitmux_num_cells_for_circuit(circuitmux_t *cmux, circuit_t *circ)
{
  chanid_circid_muxinfo_t *hashent = NULL;
  unsigned int n_cells = 0;

  tor_assert(cmux);
  tor_assert(circ);

  /* Look if it's in the circuit map */
  hashent = circuitmux_find_map_entry(cmux, circ);
  if (hashent) {
    /* Just get the cell count for this circuit */
    n_cells = hashent->muxinfo.cell_count;
  }
  /* else not attached, so 0 cells */

  return n_cells;
}

/**
 * Query total number of available cells on a circuitmux
 */

740
741
MOCK_IMPL(unsigned int,
circuitmux_num_cells, (circuitmux_t *cmux))
742
743
744
{
  tor_assert(cmux);

745
  return cmux->n_cells + cmux->destroy_cell_queue.n;
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
}

/**
 * Query total number of circuits active on a circuitmux
 */

unsigned int
circuitmux_num_active_circuits(circuitmux_t *cmux)
{
  tor_assert(cmux);

  return cmux->n_active_circuits;
}

/**
 * Query total number of circuits attached to a circuitmux
 */

unsigned int
circuitmux_num_circuits(circuitmux_t *cmux)
{
  tor_assert(cmux);

  return cmux->n_circuits;
}

772
773
774
775
776
777
778
779
/*
 * Functions for circuit code to call to update circuit status
 */

/**
 * Attach a circuit to a circuitmux, for the specified direction.
 */

780
781
782
MOCK_IMPL(void,
circuitmux_attach_circuit,(circuitmux_t *cmux, circuit_t *circ,
                           cell_direction_t direction))
783
784
785
786
787
788
789
790
791
792
793
794
795
796
{
  channel_t *chan = NULL;
  uint64_t channel_id;
  circid_t circ_id;
  chanid_circid_muxinfo_t search, *hashent = NULL;
  unsigned int cell_count;

  tor_assert(cmux);
  tor_assert(circ);
  tor_assert(direction == CELL_DIRECTION_IN ||
             direction == CELL_DIRECTION_OUT);

  /*
   * Figure out which channel we're using, and get the circuit's current
797
798
   * cell count and circuit ID; assert that the circuit is not already
   * attached to another mux.
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
   */
  if (direction == CELL_DIRECTION_OUT) {
    /* It's n_chan */
    chan = circ->n_chan;
    cell_count = circ->n_chan_cells.n;
    circ_id = circ->n_circ_id;
  } else {
    /* We want p_chan */
    chan = TO_OR_CIRCUIT(circ)->p_chan;
    cell_count = TO_OR_CIRCUIT(circ)->p_chan_cells.n;
    circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  }
  /* Assert that we did get a channel */
  tor_assert(chan);
  /* Assert that the circuit ID is sensible */
  tor_assert(circ_id != 0);

  /* Get the channel ID */
  channel_id = chan->global_identifier;

  /* See if we already have this one */
  search.chan_id = channel_id;
  search.circ_id = circ_id;
  hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
                    &search);

  if (hashent) {
    /*
     * This circuit was already attached to this cmux; make sure the
     * directions match and update the cell count and active circuit count.
     */
    log_info(LD_CIRC,
831
             "Circuit %u on channel %"PRIu64 " was already attached to "
832
             "(trying to attach to %p)",
833
             (unsigned)circ_id, (channel_id),
834
             cmux);
835
836

    /*
837
838
     * The mux pointer on this circuit and the direction in result should
     * match; otherwise assert.
839
840
841
842
843
844
845
846
     */
    tor_assert(hashent->muxinfo.direction == direction);

    /*
     * Looks okay; just update the cell count and active circuits if we must
     */
    if (hashent->muxinfo.cell_count > 0 && cell_count == 0) {
      --(cmux->n_active_circuits);
847
      circuitmux_make_circuit_inactive(cmux, circ);
848
849
    } else if (hashent->muxinfo.cell_count == 0 && cell_count > 0) {
      ++(cmux->n_active_circuits);
850
      circuitmux_make_circuit_active(cmux, circ);
851
852
853
854
855
856
857
858
859
860
    }
    cmux->n_cells -= hashent->muxinfo.cell_count;
    cmux->n_cells += cell_count;
    hashent->muxinfo.cell_count = cell_count;
  } else {
    /*
     * New circuit; add an entry and update the circuit/active circuit
     * counts.
     */
    log_debug(LD_CIRC,
861
862
             "Attaching circuit %u on channel %"PRIu64 " to cmux %p",
              (unsigned)circ_id, (channel_id), cmux);
863
864
865
866
867
868
869

    /* Insert it in the map */
    hashent = tor_malloc_zero(sizeof(*hashent));
    hashent->chan_id = channel_id;
    hashent->circ_id = circ_id;
    hashent->muxinfo.cell_count = cell_count;
    hashent->muxinfo.direction = direction;
870
    /* Allocate policy specific circuit data if we need it */
871
    if (cmux->policy->alloc_circ_data) {
872
873
874
      /* Assert that we have the means to free policy-specific data */
      tor_assert(cmux->policy->free_circ_data);
      /* Allocate it */
875
876
877
878
879
880
881
882
883
      hashent->muxinfo.policy_data =
        cmux->policy->alloc_circ_data(cmux,
                                      cmux->policy_data,
                                      circ,
                                      direction,
                                      cell_count);
      /* If we wanted policy data, it's an error  not to get any */
      tor_assert(hashent->muxinfo.policy_data);
    }
884
885
886
887
888
889
890
    HT_INSERT(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
              hashent);

    /* Update counters */
    ++(cmux->n_circuits);
    if (cell_count > 0) {
      ++(cmux->n_active_circuits);
891
      circuitmux_make_circuit_active(cmux, circ);
892
893
894
895
896
    }
    cmux->n_cells += cell_count;
  }
}

897
/**
898
899
 * Detach a circuit from a circuitmux and update all counters as needed;
 * no-op if not attached.
900
901
 */

902
903
MOCK_IMPL(void,
circuitmux_detach_circuit,(circuitmux_t *cmux, circuit_t *circ))
904
{
905
  chanid_circid_muxinfo_t search, *hashent = NULL;
906
  /*
907
908
   * Use this to keep track of whether we found it for n_chan or
   * p_chan for consistency checking.
909
910
911
   *
   * The 0 initializer is not a valid cell_direction_t value.
   * We assert that it has been replaced with a valid value before it is used.
912
   */
913
  cell_direction_t last_searched_direction = 0;
914
915
916
917
918

  tor_assert(cmux);
  tor_assert(cmux->chanid_circid_map);
  tor_assert(circ);

919
  /* See if we have it for n_chan/n_circ_id */
920
921
922
  if (circ->n_chan) {
    search.chan_id = circ->n_chan->global_identifier;
    search.circ_id = circ->n_circ_id;
923
    hashent = HT_FIND(chanid_circid_muxinfo_map, cmux->chanid_circid_map,
924
925
926
                        &search);
    last_searched_direction = CELL_DIRECTION_OUT;
  }
927

928
929
  /* Got one? If not, see if it's an or_circuit_t and try p_chan/p_circ_id */
  if (!hashent) {
930
931
932
933
    if (circ->magic == OR_CIRCUIT_MAGIC) {
      search.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
      if (TO_OR_CIRCUIT(circ)->p_chan) {
        search.chan_id = TO_OR_CIRCUIT(circ)->p_chan->global_identifier;
934
        hashent = HT_FIND(chanid_circid_muxinfo_map,
935
936
937
                            cmux->chanid_circid_map,
                            &search);
        last_searched_direction = CELL_DIRECTION_IN;
938
939
940
941
      }
    }
  }

942
943
944
  tor_assert(last_searched_direction == CELL_DIRECTION_OUT
             || last_searched_direction == CELL_DIRECTION_IN);

945
946
947
948
  /*
   * If hashent isn't NULL, we have a circuit to detach; don't remove it from
   * the map until later of circuitmux_make_circuit_inactive() breaks.
   */
949
950
951
  if (hashent) {
    /* Update counters */
    --(cmux->n_circuits);
952
953
    if (hashent->muxinfo.cell_count > 0) {
      --(cmux->n_active_circuits);
954
      /* This does policy notifies, so comes before freeing policy data */
955
      circuitmux_make_circuit_inactive(cmux, circ);
956
    }
957
    cmux->n_cells -= hashent->muxinfo.cell_count;
958

959
960
961
962
963
964
965
966
967
968
969
970
971
    /* Free policy-specific data if we have it */
    if (hashent->muxinfo.policy_data) {
      /* If we have policy data, assert that we have the means to free it */
      tor_assert(cmux->policy);
      tor_assert(cmux->policy->free_circ_data);
      /* Call free_circ_data() */
      cmux->policy->free_circ_data(cmux,
                                   cmux->policy_data,
                                   circ,
                                   hashent->muxinfo.policy_data);
      hashent->muxinfo.policy_data = NULL;
    }

972
973
    /* Consistency check: the direction must match the direction searched */
    tor_assert(last_searched_direction == hashent->muxinfo.direction);
974

975
976
977
    /* Now remove it from the map */
    HT_REMOVE(chanid_circid_muxinfo_map, cmux->chanid_circid_map, hashent);

978
979
980
    /* Wipe and free the hash entry */
    // This isn't sensitive, but we want to be sure to know if we're accessing
    // this accidentally.
981
    memwipe(hashent, 0xef, sizeof(*hashent));
982
983
    tor_free(hashent);
  }
984
985
}

986
987
988
989
990
991
/**
 * Make a circuit active; update active list and policy-specific info, but
 * we don't mess with the counters or hash table here.
 */

static void
992
circuitmux_make_circuit_active(circuitmux_t *cmux, circuit_t *circ)
993
994
{
  tor_assert(cmux);
995
  tor_assert(cmux->policy);
996
997
  tor_assert(circ);

998
  /* Policy-specific notification */
999
  if (cmux->policy->notify_circ_active) {
1000
    /* Okay, we need to check the circuit for policy data now */
1001
    chanid_circid_muxinfo_t *hashent = circuitmux_find_map_entry(cmux, circ);
1002
1003
    /* We should have found something */
    tor_assert(hashent);
1004
1005
1006
    /* Notify */
    cmux->policy->notify_circ_active(cmux, cmux->policy_data,
                                     circ, hashent->muxinfo.policy_data);
1007
  }
1008
1009
1010
1011
1012
1013
1014
1015
}

/**
 * Make a circuit inactive; update active list and policy-specific info, but
 * we don't mess with the counters or hash table here.
 */

static void
1016
circuitmux_make_circuit_inactive(circuitmux_t *cmux, circuit_t *circ)
1017
1018
{
  tor_assert(cmux);
1019
  tor_assert(cmux->policy);
1020
1021
  tor_assert(circ);

1022
  /* Policy-specific notification */
1023
  if (cmux->policy->notify_circ_inactive) {
1024
    /* Okay, we need to check the circuit for policy data now */
1025
    chanid_circid_muxinfo_t *hashent = circuitmux_find_map_entry(cmux, circ);
1026
1027
    /* We should have found something */
    tor_assert(hashent);
1028
1029
1030
    /* Notify */
    cmux->policy->notify_circ_inactive(cmux, cmux->policy_data,
                                       circ, hashent->muxinfo.policy_data);
1031
  }
1032
1033
}

1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
/**
 * Clear the cell counter for a circuit on a circuitmux
 */

void
circuitmux_clear_num_cells(circuitmux_t *cmux, circuit_t *circ)
{
  /* This is the same as setting the cell count to zero */
  circuitmux_set_num_cells(cmux, circ, 0);
}

/**
 * Set the cell counter for a circuit on a circuitmux
 */

void
circuitmux_set_num_cells(circuitmux_t *cmux, circuit_t *circ,
                         unsigned int n_cells)
{
  chanid_circid_muxinfo_t *hashent = NULL;

  tor_assert(cmux);
  tor_assert(circ);

  /* Search for this circuit's entry */
  hashent = circuitmux_find_map_entry(cmux, circ);
  /* Assert that we found one */
  tor_assert(hashent);

  /* Update cmux cell counter */
  cmux->n_cells -= hashent->muxinfo.cell_count;
  cmux->n_cells += n_cells;

1067
  /* Do we need to notify a cmux policy? */
1068
  if (cmux->policy->notify_set_n_cells) {
1069
    /* Call notify_set_n_cells */
1070
1071
1072
1073
1074
1075
1076
    cmux->policy->notify_set_n_cells(cmux,
                                     cmux->policy_data,
                                     circ,
                                     hashent->muxinfo.policy_data,
                                     n_cells);
  }

1077
1078
1079
1080
1081
1082
  /*
   * Update cmux active circuit counter: is the old cell count > 0 and the
   * new cell count == 0 ?
   */
  if (hashent->muxinfo.cell_count > 0 && n_cells == 0) {
    --(cmux->n_active_circuits);
1083
    hashent->muxinfo.cell_count = n_cells;
1084
    circuitmux_make_circuit_inactive(cmux, circ);
1085
1086
1087
  /* Is the old cell count == 0 and the new cell count > 0 ? */
  } else if (hashent->muxinfo.cell_count == 0 && n_cells > 0) {
    ++(cmux->n_active_circuits);
1088
    hashent->muxinfo.cell_count = n_cells;
1089
    circuitmux_make_circuit_active(cmux, circ);
1090
1091
  } else {
    hashent->muxinfo.cell_count = n_cells;
1092
1093
1094
  }
}

1095
1096
1097
1098
1099
/*
 * Functions for channel code to call to get a circuit to transmit from or
 * notify that cells have been transmitted.
 */

1100
1101
1102
/**
 * Pick a circuit to send from, using the active circuits list or a
 * circuitmux policy if one is available.  This is called from channel.c.
1103
1104
1105
1106
1107
1108
 *
 * If we would rather send a destroy cell, return NULL and set
 * *<b>destroy_queue_out</b> to the destroy queue.
 *
 * If we have nothing to send, set *<b>destroy_queue_out</b> to NULL and
 * return NULL.
1109
1110
1111
 */

circuit_t *
1112
circuitmux_get_first_active_circuit(circuitmux_t *cmux,
1113
                                    destroy_cell_queue_t **destroy_queue_out)
1114
1115
1116
1117
{
  circuit_t *circ = NULL;

  tor_assert(cmux);
1118
1119
1120
  tor_assert(cmux->policy);
  /* This callback is mandatory. */
  tor_assert(cmux->policy->pick_active_circuit);
1121
1122
1123
1124
1125
1126
1127
1128
  tor_assert(destroy_queue_out);

  *destroy_queue_out = NULL;

  if (cmux->destroy_cell_queue.n &&
        (!cmux->last_cell_was_destroy || cmux->n_active_circuits == 0)) {
    /* We have destroy cells to send, and either we just sent a relay cell,
     * or we have no relay cells to send. */
1129

1130
1131
1132
1133
1134
1135
    /* XXXX We should let the cmux policy have some say in this eventually. */
    /* XXXX Alternating is not a terribly brilliant approach here. */
    *destroy_queue_out = &cmux->destroy_cell_queue;

    cmux->last_cell_was_destroy = 1;
  } else if (cmux->n_active_circuits > 0) {
1136
1137
1138
    /* We also must have a cell available for this to be the case */
    tor_assert(cmux->n_cells > 0);
    /* Do we have a policy-provided circuit selector? */
1139
    circ = cmux->policy->pick_active_circuit(cmux, cmux->policy_data);
1140
1141
1142
1143
1144
    cmux->last_cell_was_destroy = 0;
  } else {
    tor_assert(cmux->n_cells == 0);
    tor_assert(cmux->destroy_cell_queue.n == 0);
  }
1145
1146
1147
1148

  return circ;
}

1149
1150
1151
1152
1153
/**
 * Notify the circuitmux that cells have been sent on a circuit; this
 * is called from channel.c.
 */

1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
void
circuitmux_notify_xmit_cells(circuitmux_t *cmux, circuit_t *circ,
                             unsigned int n_cells)
{
  chanid_circid_muxinfo_t *hashent = NULL;
  int becomes_inactive = 0;

  tor_assert(cmux);
  tor_assert(circ);

  if (n_cells == 0) return;

  /*
   * To handle this, we have to:
   *
   * 1.) Adjust the circuit's cell counter in the cmux hash table
   * 2.) Move the circuit to the tail of the active_circuits linked list
   *     for this cmux, or make the circuit inactive if the cell count
   *     went to zero.
   * 3.) Call cmux->policy->notify_xmit_cells(), if any
   */

  /* Find the hash entry */
  hashent = circuitmux_find_map_entry(cmux, circ);
  /* Assert that we found one */
  tor_assert(hashent);

  /* Adjust the cell counter and assert that we had that many cells to send */
  tor_assert(n_cells <= hashent->muxinfo.cell_count);
  hashent->muxinfo.cell_count -= n_cells;
  /* Do we need to make the circuit inactive? */
  if (hashent->muxinfo.cell_count == 0) becomes_inactive = 1;
1186
1187
  /* Adjust the mux cell counter */
  cmux->n_cells -= n_cells;
1188
1189
1190
1191
1192

  /*
   * We call notify_xmit_cells() before making the circuit inactive if needed,
   * so the policy can always count on this coming in on an active circuit.
   */
1193
  if (cmux->policy->notify_xmit_cells) {
1194
1195
1196
1197
1198
1199
1200
    cmux->policy->notify_xmit_cells(cmux, cmux->policy_data, circ,
                                    hashent->muxinfo.policy_data,
                                    n_cells);
  }

  /*
   * Now make the circuit inactive if needed; this will call the policy's
1201
   * notify_circ_inactive() if present.
1202
1203
1204
   */
  if (becomes_inactive) {
    --(cmux->n_active_circuits);
1205
    circuitmux_make_circuit_inactive(cmux, circ);
1206
1207
1208
  }
}

1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
/**
 * Notify the circuitmux that a destroy was sent, so we can update
 * the counter.
 */

void
circuitmux_notify_xmit_destroy(circuitmux_t *cmux)
{
  tor_assert(cmux);

  --(cmux->destroy_ctr);
  --(global_destroy_ctr);
  log_debug(LD_CIRC,
1222
1223
            "Cmux at %p sent a destroy, cmux counter is now %"PRId64", "
            "global counter is now %"PRId64,
1224
            cmux,
1225
1226
            (cmux->destroy_ctr),
            (global_destroy_ctr));
1227
1228
}

1229
1230
1231
1232
1233
1234
1235
/*DOCDOC */
void
circuitmux_append_destroy_cell(channel_t *chan,
                               circuitmux_t *cmux,
                               circid_t circ_id,
                               uint8_t reason)
{
1236
  destroy_cell_queue_append(&cmux->destroy_cell_queue, circ_id, reason);
1237

1238
1239
1240
1241
  /* Destroy entering the queue, update counters */
  ++(cmux->destroy_ctr);
  ++global_destroy_ctr;
  log_debug(LD_CIRC,
1242
            "Cmux at %p queued a destroy for circ %u, cmux counter is now "
1243
            "%"PRId64", global counter is now %"PRId64,
1244
            cmux, circ_id,
1245
1246
            (cmux->destroy_ctr),
            (global_destroy_ctr));
1247

1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
  /* XXXX Duplicate code from append_cell_to_circuit_queue */
  if (!channel_has_queued_writes(chan)) {
    /* There is no data at all waiting to be sent on the outbuf.  Add a
     * cell, so that we can notice when it gets flushed, flushed_some can
     * get called, and we can start putting more data onto the buffer then.
     */
    log_debug(LD_GENERAL, "Primed a buffer.");
    channel_flush_from_first_active_circuit(chan, 1);
  }
}
1258

Nick Mathewson's avatar
Nick Mathewson committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
/*DOCDOC; for debugging 12184.  This runs slowly. */
int64_t
circuitmux_count_queued_destroy_cells(const channel_t *chan,
                                      const circuitmux_t *cmux)
{
  int64_t n_destroy_cells = cmux->destroy_ctr;
  int64_t destroy_queue_size = cmux->destroy_cell_queue.n;

  int64_t manual_total = 0;
  int64_t manual_total_in_map = 0;
1269
  destroy_cell_t *cell;
Nick Mathewson's avatar
Nick Mathewson committed
1270
1271
1272
1273
1274

  TOR_SIMPLEQ_FOREACH(cell, &cmux->destroy_cell_queue.head, next) {
    circid_t id;
    ++manual_total;

1275
    id = cell->circid;
Nick Mathewson's avatar
Nick Mathewson committed
1276
1277
1278
1279
1280
1281
1282
1283
    if (circuit_id_in_use_on_channel(id, (channel_t*)chan))
      ++manual_total_in_map;
  }

  if (n_destroy_cells != destroy_queue_size ||
      n_destroy_cells != manual_total ||
      n_destroy_cells != manual_total_in_map) {
    log_warn(LD_BUG, "  Discrepancy in counts for queued destroy cells on "
1284
1285
1286
1287
1288
1289
             "circuitmux. n=%"PRId64". queue_size=%"PRId64". "
             "manual_total=%"PRId64". manual_total_in_map=%"PRId64".",
             (n_destroy_cells),
             (destroy_queue_size),
             (manual_total),
             (manual_total_in_map));
Nick Mathewson's avatar
Nick Mathewson committed
1290
1291
1292
1293
  }

  return n_destroy_cells;
}
Nick Mathewson's avatar
Nick Mathewson committed
1294

1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
/**
 * Compare cmuxes to see which is more preferred; return < 0 if
 * cmux_1 has higher priority (i.e., cmux_1 < cmux_2 in the scheduler's
 * sort order), > 0 if cmux_2 has higher priority, or 0 if they are
 * equally preferred.
 *
 * If the cmuxes have different cmux policies or the policy does not
 * support the cmp_cmux method, return 0.
 */

1305
1306
MOCK_IMPL(int,
circuitmux_compare_muxes, (circuitmux_t *cmux_1, circuitmux_t *cmux_2))
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
{
  const circuitmux_policy_t *policy;

  tor_assert(cmux_1);
  tor_assert(cmux_2);

  if (cmux_1 == cmux_2) {
    /* Equivalent because they're the same cmux */
    return 0;
  }

  if (cmux_1->policy && cmux_2->policy) {
    if (cmux_1->policy == cmux_2->policy) {
      policy = cmux_1->policy;

      if (policy->cmp_cmux) {
        /* Okay, we can compare! */
        return policy->cmp_cmux(cmux_1, cmux_1->policy_data,
                                cmux_2, cmux_2->policy_data);
      } else {
        /*
         * Equivalent because the policy doesn't know how to compare between
         * muxes.
         */
        return 0;
      }
    } else {
      /* Equivalent because they have different policies */
      return 0;
    }
  } else {
    /* Equivalent because one or both are missing a policy */
    return 0;
  }
}