blob: 312b49d933a2c246eaf4fd959137451f7e941a1c [file] [log] [blame]
Matthew McClintocke1bcfe42013-11-22 15:33:09 -06001#include <netlink/genl/genl.h>
2#include <errno.h>
3#include <stdio.h>
4#include <arpa/inet.h>
5
6#include "fast-classifier-priv.h"
7
8
9void fast_classifier_ipv4_offload(unsigned char proto, unsigned long src_saddr,
10 unsigned long dst_saddr, unsigned short sport,
11 unsigned short dport) {
12 struct nl_sock *sock;
13 struct nl_msg *msg;
14 int family;
15 int ret;
16 char src_str[INET_ADDRSTRLEN];
17 char dst_str[INET_ADDRSTRLEN];
18 struct fast_classifier_msg fc_msg;
19
Matthew McClintockda8e5922013-11-26 00:09:50 -060020#ifdef DEBUG
Matthew McClintocke1bcfe42013-11-22 15:33:09 -060021 printf("DEBUG: would offload: %d, %s, %s, %d, %d\n", proto,
22 inet_ntop(AF_INET, &src_saddr, src_str, INET_ADDRSTRLEN),
23 inet_ntop(AF_INET, &dst_saddr, dst_str, INET_ADDRSTRLEN),
24 sport, dport);
Matthew McClintockda8e5922013-11-26 00:09:50 -060025#endif
Matthew McClintocke1bcfe42013-11-22 15:33:09 -060026
27 fc_msg.proto = proto;
28 fc_msg.src_saddr = src_saddr;
29 fc_msg.dst_saddr = dst_saddr;
30 fc_msg.sport = sport;
31 fc_msg.dport = dport;
32
33 sock = nl_socket_alloc();
34 if (sock == NULL) {
35 printf("Unable to allocate socket.\n");
36 return;
37 }
38
39 genl_connect(sock);
40
41 family = genl_ctrl_resolve(sock, "FAST_CLASSIFIER");
42 if (family < 0) {
43 nl_socket_free(sock);
44 printf("Unable to resolve family\n");
45 return;
46 }
47
48 msg = nlmsg_alloc();
49 if (msg == NULL) {
50 nl_socket_free(sock);
51 printf("Unable to allocate message\n");
52 return;
53 }
54
55 genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0,
56 NLM_F_REQUEST, 1, 1);
57 nla_put(msg, 1, sizeof(fc_msg), &fc_msg);
58
59 ret = nl_send_auto_complete(sock, msg);
60
61 nlmsg_free(msg);
62 if (ret < 0) {
63 printf("nlmsg_free failed");
64 nl_close(sock);
65 nl_socket_free(sock);
66 return;
67 }
68
69 ret = nl_wait_for_ack(sock);
70 if (ret < 0) {
71 printf("wait for ack failed");
72 nl_close(sock);
73 nl_socket_free(sock);
74 return;
75 }
76
77 nl_close(sock);
78 nl_socket_free(sock);
79}