TAS
TCP Acceleration as an OS Service
routing.c
1 /*
2  * Copyright 2019 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 <stdlib.h>
26 #include <stdio.h>
27 
28 #include <tas.h>
29 #include "internal.h"
30 
34  uint32_t dest_ip;
36  uint32_t dest_mask;
38  uint32_t next_hop;
39 };
40 
41 static inline uint32_t prefix_len_mask(uint8_t len);
42 static inline struct routing_table_entry *resolve(uint32_t ip);
43 
45 static struct routing_table_entry *routing_table = NULL;
46 static size_t routing_table_len = 0;
47 
48 int routing_init(void)
49 {
50  struct config_route *cr;
51  size_t i;
52  uint32_t mask;
53 
54  /* count number of entries to be added */
55  routing_table_len = 1;
56  for (cr = config.routes; cr != NULL; routing_table_len++, cr = cr->next);
57 
58  /* allocate table */
59  if ((routing_table = calloc(routing_table_len, sizeof(*routing_table)))
60  == NULL)
61  {
62  fprintf(stderr, "routing_init: allocating routing table failed\n");
63  return -1;
64  }
65 
66  /* first fill in network route based on ip and prefix */
67  mask = prefix_len_mask(config.ip_prefix);
68  routing_table[0].dest_ip = config.ip & mask;
69  routing_table[0].dest_mask = mask;
70  routing_table[0].next_hop = 0;
71 
72  /* fill in routing table */
73  for (i = 1, cr = config.routes; cr != NULL; i++, cr = cr->next) {
74  mask = prefix_len_mask(cr->ip_prefix);
75  if ((mask & cr->ip) != cr->ip) {
76  fprintf(stderr, "routing_init: mask removes non-0 bits "
77  "(d=%x m=%x n=%x)\n", cr->ip, mask, cr->next_hop_ip);
78  return -1;
79  }
80 
81  routing_table[i].dest_ip = cr->ip;
82  routing_table[i].dest_mask = mask;
83  routing_table[i].next_hop = cr->next_hop_ip;
84  }
85 
86  return 0;
87 }
88 
89 int routing_resolve(struct nicif_completion *comp, uint32_t ip, uint64_t *mac)
90 {
91  struct routing_table_entry *rte;
92 
93  while (1) {
94  rte = resolve(ip);
95  if (rte == NULL) {
96  fprintf(stderr, "routing_resolve: routing failed\n");
97  return -1;
98  }
99 
100  if (rte->next_hop == 0) {
101  break;
102  }
103 
104  ip = rte->next_hop;
105  }
106 
107  return arp_request(comp, ip, mac);
108 }
109 
110 static inline uint32_t prefix_len_mask(uint8_t len)
111 {
112  return ~((1ULL << (32 - len)) - 1);
113 }
114 
115 static inline struct routing_table_entry *resolve(uint32_t ip)
116 {
117  size_t i;
118 
119  for (i = 0; i < routing_table_len; i++) {
120  if (routing_table[i].dest_ip == (ip & routing_table[i].dest_mask)) {
121  return &routing_table[i];
122  }
123  }
124 
125  return NULL;
126 }
uint8_t ip_prefix
Definition: config.h:147
int arp_request(struct nicif_completion *comp, uint32_t ip, uint64_t *mac)
Definition: arp.c:81
uint8_t ip_prefix
Definition: config.h:69
struct config_route * routes
Definition: config.h:71
uint32_t next_hop
Definition: routing.c:38
uint32_t dest_ip
Definition: routing.c:34
uint32_t ip
Definition: config.h:145
uint32_t dest_mask
Definition: routing.c:36
struct config_route * next
Definition: config.h:151
uint32_t next_hop_ip
Definition: config.h:149
uint32_t ip
Definition: config.h:67
int routing_init(void)
Definition: routing.c:48
Definition: routing.c:32
int routing_resolve(struct nicif_completion *comp, uint32_t ip, uint64_t *mac)
Definition: routing.c:89