TAS
TCP Acceleration as an OS Service
blocking.c
1 /*
2  * Copyright 2020 University of Washington, Max Planck Institute for
3  * Software Systems, and The University of Texas at Austin
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <unistd.h>
29 
30 #include <tas.h>
31 
32 extern int kernel_notifyfd;
33 
34 static void notify_core(int cfd, uint64_t *last_ts, uint64_t tsc,
35  uint64_t delta)
36 {
37  uint64_t val;
38 
39  /* blocking is disabled */
40  if (delta == UINT64_MAX) {
41  return;
42  }
43 
44  if(tsc - *last_ts > delta) {
45  val = 1;
46  if (write(cfd, &val, sizeof(uint64_t)) != sizeof(uint64_t)) {
47  perror("notify_core: write failed");
48  abort();
49  }
50  }
51 
52  *last_ts = tsc;
53 }
54 
55 void notify_fastpath_core(unsigned core)
56 {
57  notify_core(fp_state->kctx[core].evfd, &fp_state->kctx[core].last_ts,
58  util_rdtsc(), tas_info->poll_cycle_tas);
59 }
60 
61 void notify_app_core(int appfd, uint64_t *last_ts)
62 {
63  notify_core(appfd, last_ts, util_rdtsc(), tas_info->poll_cycle_app);
64 }
65 
66 void notify_appctx(struct flextcp_pl_appctx *ctx, uint64_t tsc)
67 {
68  notify_core(ctx->evfd, &ctx->last_ts, tsc, tas_info->poll_cycle_app);
69 }
70 
71 void notify_slowpath_core(void)
72 {
73  static uint64_t __thread last_ts = 0;
74  notify_core(kernel_notifyfd, &last_ts, util_rdtsc(),
75  tas_info->poll_cycle_tas);
76 }
77 
78 int notify_canblock(struct notify_blockstate *nbs, int had_data, uint64_t tsc)
79 {
80  if (tas_info->poll_cycle_tas == UINT64_MAX) {
81  return 0;
82  }
83 
84  if (had_data) {
85  /* not idle this round, reset everything */
86  nbs->can_block = nbs->second_bar = 0;
87  nbs->last_active_ts = tsc;
88  } else if (nbs->second_bar) {
89  /* we can block now, reset afterwards */
90  nbs->can_block = nbs->second_bar = 0;
91  nbs->last_active_ts = tsc;
92  return 1;
93  } else if (nbs->can_block &&
94  tsc - nbs->last_active_ts > tas_info->poll_cycle_tas)
95  {
96  /* we've reached the poll cycle interval, so just poll once more */
97  nbs->second_bar = 1;
98  } else {
99  /* waiting for poll cycle interval */
100  nbs->can_block = 1;
101  }
102 
103  return 0;
104 }
105 
106 void notify_canblock_reset(struct notify_blockstate *nbs)
107 {
108  nbs->can_block = nbs->second_bar = 0;
109 }
uint64_t poll_cycle_app
Definition: tas_memif.h:66
uint64_t poll_cycle_tas
Definition: tas_memif.h:68