1 /*
2     Copyright (c) 2014-2016 Intel Corporation.  All Rights Reserved.
3 
4     Redistribution and use in source and binary forms, with or without
5     modification, are permitted provided that the following conditions
6     are met:
7 
8       * Redistributions of source code must retain the above copyright
9         notice, this list of conditions and the following disclaimer.
10       * Redistributions in binary form must reproduce the above copyright
11         notice, this list of conditions and the following disclaimer in the
12         documentation and/or other materials provided with the distribution.
13       * Neither the name of Intel Corporation nor the names of its
14         contributors may be used to endorse or promote products derived
15         from this software without specific prior written permission.
16 
17     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21     HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 
31 #ifndef OFFLOAD_ENV_H_INCLUDED
32 #define OFFLOAD_ENV_H_INCLUDED
33 
34 #include <list>
35 #include "offload_util.h"
36 
37 // data structure and routines to parse MIC user environment and pass to MIC
38 
39 enum MicEnvVarKind
40 {
41     c_no_mic,         // not MIC env var
42     c_mic_var,        // for <mic-prefix>_<var>
43     c_mic_card_var,   // for <mic-prefix>_<card-number>_<var>
44     c_mic_card_env    // for <mic-prefix>_<card-number>_ENV
45 };
46 
47 struct DLL_LOCAL MicEnvVar {
48 public:
MicEnvVarMicEnvVar49     MicEnvVar() : prefix(0) {}
50     ~MicEnvVar();
51 
52     void analyze_env_var(char *env_var_string);
53     char** create_environ_for_card(int card_num);
54     MicEnvVarKind get_env_var_kind(
55         char *env_var_string,
56         int *card_number,
57         char **env_var_name,
58         int *env_var_name_length,
59         char **env_var_def
60     );
61     void add_env_var(
62         int card_number,
63         char *env_var_name,
64         int env_var_name_length,
65         char *env_var_def
66     );
67 
set_prefixMicEnvVar68     void set_prefix(const char *pref) {
69         prefix = (pref && *pref != '\0') ? pref : 0;
70     }
71 
72     struct VarValue {
73     public:
74         char* env_var;
75         int   length;
76         char* env_var_value;
77 
VarValueMicEnvVar::VarValue78         VarValue(char* var, int ln, char* value)
79         {
80             env_var = var;
81             length = ln;
82             env_var_value = value;
83         }
84         ~VarValue();
85     };
86 
87     struct CardEnvVars {
88     public:
89 
90         int card_number;
91         std::list<struct VarValue*> env_vars;
92 
CardEnvVarsMicEnvVar::CardEnvVars93         CardEnvVars() { card_number = any_card; }
CardEnvVarsMicEnvVar::CardEnvVars94         CardEnvVars(int num) { card_number = num; }
95         ~CardEnvVars();
96 
97         void add_new_env_var(int number, char *env_var, int length,
98                              char *env_var_value);
99         VarValue* find_var(char* env_var_name, int env_var_name_length);
100     };
101     static const int any_card;
102 
103 private:
104     void         mic_parse_env_var_list(int card_number, char *env_var_def);
105     CardEnvVars* get_card(int number);
106 
107     const char *prefix;
108     std::list<struct CardEnvVars *> card_spec_list;
109     CardEnvVars common_vars;
110 };
111 
112 #endif // OFFLOAD_ENV_H_INCLUDED
113