vCGNAPT VNF initial check-in
[samplevnf.git] / VNFs / vCGNAPT / pipeline / cgnapt_pcp_fe.c
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <cmdline_parse.h>
18 #include <cmdline_parse_num.h>
19 #include <cmdline_parse_string.h>
20 #include <cmdline_parse_ipaddr.h>
21 #include <cmdline_parse_etheraddr.h>
22
23 #include "app.h"
24 #include "pipeline_common_fe.h"
25 #include "pipeline_cgnapt.h"
26 #include "pipeline_cgnapt_common.h"
27 #include "cgnapt_pcp_fe.h"
28 #include "cgnapt_pcp_be.h"
29
30 #ifdef PCP_ENABLE
31
32 /**
33  * @file
34  * Pipeline CG-NAPT PCP FE Implementation.
35  *
36  * Implementation of Pipeline CG-NAPT PCP Front End (FE).
37  * Provides CLI support.
38  * Runs on master core.
39  *
40  */
41
42 void cmd_pcp_parsed(
43         void *parsed_result,
44          __rte_unused struct cmdline *cl,
45         void *data);
46 /**
47  * A structure defining PCP cmd parse arguments.
48  */
49 struct cmd_pcp_result {
50         cmdline_fixed_string_t p_string;
51         uint32_t p;
52         cmdline_fixed_string_t pcp_string;
53         uint8_t cmd;
54         uint32_t lifetime;
55 };
56
57 static cmdline_parse_token_string_t cmd_pcp_p_string =
58 TOKEN_STRING_INITIALIZER(struct cmd_pcp_result, p_string, "p");
59
60 static cmdline_parse_token_num_t cmd_pcp_p =
61 TOKEN_NUM_INITIALIZER(struct cmd_pcp_result, p, UINT32);
62
63 static cmdline_parse_token_string_t cmd_pcp_string =
64 TOKEN_STRING_INITIALIZER(struct cmd_pcp_result,
65                          pcp_string, "pcp");
66
67 static cmdline_parse_token_num_t cmd_pcp_cmd =
68 TOKEN_NUM_INITIALIZER(struct cmd_pcp_result, cmd, UINT8);
69
70 static cmdline_parse_token_num_t cmd_pcp_lifetime =
71 TOKEN_NUM_INITIALIZER(struct cmd_pcp_result, lifetime, UINT32);
72
73 cmdline_parse_inst_t cmd_pcp = {
74         .f = cmd_pcp_parsed,
75         .data = NULL,
76         .help_str = "NAPT PCP cmd",
77         .tokens = {
78                          (void *) &cmd_pcp_p_string,
79                          (void *) &cmd_pcp_p,
80                          (void *) &cmd_pcp_string,
81                          (void *) &cmd_pcp_cmd,
82                          (void *) &cmd_pcp_lifetime,
83                          NULL,
84                          },
85 };
86
87  /**
88  * Function to send a PCP cmd message to BE
89  *
90  * @param app
91  *  A pointer to pipeline app
92  * @param pipeline_id
93  *  Pipeline id
94  * @param cmd
95  *  PCP specific command whether to show stats,set to get lifetime
96  * @param lifetime
97  *      PCP entry lifetime
98  * @return
99  *  0 on success, negative on error.
100  */
101 //#ifdef PCP_ENABLE
102 static int
103 app_pipeline_cgnapt_pcp(struct app_params *app,
104                         uint32_t pipeline_id, uint8_t cmd, uint32_t lifetime){
105
106         struct pipeline_cgnapt *p;
107         struct pipeline_cgnapt_pcp_msg_req *req;
108         struct pipeline_cgnapt_pcp_msg_rsp *rsp;
109
110         /* Check input arguments */
111         if (app == NULL)
112                 return -1;
113
114         p = app_pipeline_data_fe(app, pipeline_id,
115                         (struct pipeline_type *)&pipeline_cgnapt);
116         if (p == NULL)
117                 return -1;
118
119         /* Allocate and write request */
120         req = app_msg_alloc(app);
121         if (req == NULL)
122                 return -1;
123
124         req->type = PIPELINE_MSG_REQ_CUSTOM;
125         req->subtype = PIPELINE_CGNAPT_MSG_REQ_PCP;
126         req->cmd = cmd;
127         req->lifetime = lifetime;
128
129         rsp = app_msg_send_recv(app, pipeline_id, req, MSG_TIMEOUT_DEFAULT);
130         if (rsp == NULL)
131                 return -1;
132
133         /* Read response */
134         if (rsp->status) {
135                 app_msg_free(app, rsp);
136                 printf("Error rsp->status %d\n", rsp->status);
137                 return -1;
138         }
139
140         /* Free response */
141         app_msg_free(app, rsp);
142
143         return 0;
144 }
145
146 /**
147  * Helping function for PCP cmd
148  *
149  * @param parsed_result
150  *  A pointer parsed add arguments
151  * @param cl
152  *  unused pointer to struct cmdline
153  * @param data
154  *  void pointer data
155  */
156 void
157 cmd_pcp_parsed(void *parsed_result,
158         __rte_unused struct cmdline *cl,
159         void *data)
160 {
161         struct cmd_pcp_result *params = parsed_result;
162         struct app_params *app = data;
163         int status;
164
165         status = app_pipeline_cgnapt_pcp(app, params->p, params->cmd,
166                         params->lifetime);
167
168         if (status != 0) {
169                 printf("PCP Command failed\n");
170                 return;
171         }
172 }
173
174 #endif