Skip to content

Commit df50614

Browse files
committed
Rework error handling.
1 parent d749da2 commit df50614

2 files changed

Lines changed: 49 additions & 34 deletions

File tree

src/tech/af-xdp/af_xdp.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,14 @@ xsk_socket_info_t *tech_afxdp__sock_setup(const char *dev, u16 thread_id, int ve
562562
void tech_afxdp__sock_cleanup(xsk_socket_info_t *xsk)
563563
{
564564
// If the AF_XDP/XSK socket isn't NULL, delete it.
565-
if (xsk->xsk != NULL)
565+
if (xsk != NULL && xsk->xsk != NULL)
566566
{
567567
xsk_socket__delete(xsk->xsk);
568-
}
569568

570-
// If the UMEM isn't NULL, delete it.
571-
if (xsk->umem != NULL)
572-
{
573-
xsk_umem__delete(xsk->umem->umem);
569+
// If the UMEM isn't NULL, delete it.
570+
if (xsk->umem != NULL)
571+
{
572+
xsk_umem__delete(xsk->umem->umem);
573+
}
574574
}
575575
}

src/tech/sequence.c

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ static void *sequence__thread_core(void *temp)
102102
// Get human-friendly sequence ID (id + 1).
103103
int seq_num = ti->seq_cnt + 1;
104104

105+
// Initialize socket FD.
106+
int sock_fd;
107+
108+
// Create AF_XDP socket and check.
109+
xsk_socket_info_t *xsk = tech_afxdp__sock_setup(ti->device, ti->id, ti->cmd.verbose);
110+
111+
if (xsk == NULL)
112+
{
113+
fprintf(stderr, "[%d] Error setting up AF_XDP socket on thread.\n", seq_num);
114+
115+
goto thread_exit;
116+
}
117+
118+
sock_fd = tech_afxdp__sock_fd(xsk);
119+
120+
if (sock_fd < 0)
121+
{
122+
fprintf(stderr, "[%d] Error setting up AF_XDP socket on thread.\n", seq_num);
123+
124+
goto thread_exit;
125+
}
126+
105127
// Let's parse some config values before creating the socket so we know what we're doing.
106128
u8 protocol = IPPROTO_UDP;
107129
u8 src_mac[ETH_ALEN] = {0};
@@ -116,11 +138,16 @@ static void *sequence__thread_core(void *temp)
116138
{
117139
fprintf(stderr, "[%d] Failed to allocate buffers\n", seq_num);
118140

119-
free(data_len);
120-
free(pckt_len);
121-
free(buffer);
141+
if (data_len != NULL)
142+
free(data_len);
143+
144+
if (pckt_len != NULL)
145+
free(pckt_len);
146+
147+
if (buffer != NULL)
148+
free(buffer);
122149

123-
pthread_exit(NULL);
150+
goto thread_exit;
124151
}
125152

126153
// Payloads.
@@ -137,7 +164,7 @@ static void *sequence__thread_core(void *temp)
137164
{
138165
fprintf(stderr, "[%d] Failed to intialize payloads array due to allocation error.\n", seq_num);
139166

140-
pthread_exit(NULL);
167+
goto thread_exit;
141168
}
142169

143170
// Let's first start off by checking if the source MAC address is set within the config.
@@ -162,29 +189,6 @@ static void *sequence__thread_core(void *temp)
162189
protocol = IPPROTO_ICMP;
163190
}
164191

165-
// Initialize socket FD.
166-
int sock_fd;
167-
168-
// Create AF_XDP socket and check.
169-
xsk_socket_info_t *xsk = tech_afxdp__sock_setup(ti->device, ti->id, ti->cmd.verbose);
170-
171-
sock_fd = tech_afxdp__sock_fd(xsk);
172-
173-
if (sock_fd < 0)
174-
{
175-
fprintf(stderr, "[%d] Error setting up AF_XDP socket on thread.\n", seq_num);
176-
177-
// Attempt to cleanup socket.
178-
tech_afxdp__sock_cleanup(xsk);
179-
180-
// Attempt to close the socket.
181-
close(sock_fd);
182-
183-
pthread_exit(NULL);
184-
185-
return NULL;
186-
}
187-
188192
// Check if source MAC address is set properly. If not, let's get the MAC address of the interface we're sending packets out of.
189193
if (src_mac[0] == 0 && src_mac[1] == 0 && src_mac[2] == 0 && src_mac[3] == 0 && src_mac[4] == 0 && src_mac[5] == 0)
190194
{
@@ -930,6 +934,17 @@ static void *sequence__thread_core(void *temp)
930934
// Free payloads.
931935
free(payloads);
932936

937+
pthread_exit(NULL);
938+
939+
thread_exit:
940+
// Cleanup AF_XDP socket.
941+
tech_afxdp__sock_cleanup(xsk);
942+
943+
if (sock_fd > -1)
944+
{
945+
close(sock_fd);
946+
}
947+
933948
pthread_exit(NULL);
934949
}
935950

0 commit comments

Comments
 (0)