1 /*
2     Domain communications for Xen Store Daemon.
3     Copyright (C) 2005 Rusty Russell IBM Corporation
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef _XENSTORED_DOMAIN_H
20 #define _XENSTORED_DOMAIN_H
21 
22 /*
23  * All accounting data is stored in a per-domain array.
24  * Depending on the account item there might be other scopes as well, like e.g.
25  * a per transaction array.
26  */
27 enum accitem {
28 	ACC_NODES,
29 	ACC_REQ_N,		/* Number of elements per request. */
30 	ACC_TR_N = ACC_REQ_N,	/* Number of elements per transaction. */
31 	ACC_CHD_N = ACC_TR_N,	/* max(ACC_REQ_N, ACC_TR_N), for changed dom. */
32 	ACC_WATCH = ACC_TR_N,
33 	ACC_OUTST,
34 	ACC_MEM,
35 	ACC_TRANS,
36 	ACC_TRANSNODES,
37 	ACC_NPERM,
38 	ACC_PATHLEN,
39 	ACC_NODESZ,
40 	ACC_N,			/* Number of elements per domain. */
41 };
42 
43 struct quota {
44 	const char *name;
45 	const char *descr;
46 	unsigned int val;
47 	unsigned int max;
48 };
49 
50 extern struct quota hard_quotas[ACC_N];
51 extern struct quota soft_quotas[ACC_N];
52 
53 void handle_event(void);
54 
55 void check_domains(void);
56 
57 /* domid, mfn, eventchn, path */
58 int do_introduce(const void *ctx, struct connection *conn,
59 		 struct buffered_data *in);
60 
61 /* domid */
62 int do_is_domain_introduced(const void *ctx, struct connection *conn,
63 			    struct buffered_data *in);
64 
65 /* domid */
66 int do_release(const void *ctx, struct connection *conn,
67 	       struct buffered_data *in);
68 
69 /* domid */
70 int do_resume(const void *ctx, struct connection *conn,
71 	      struct buffered_data *in);
72 
73 /* domid, target */
74 int do_set_target(const void *ctx, struct connection *conn,
75 		  struct buffered_data *in);
76 
77 /* domid */
78 int do_get_domain_path(const void *ctx, struct connection *conn,
79 		       struct buffered_data *in);
80 
81 /* Allow guest to reset all watches */
82 int do_reset_watches(const void *ctx, struct connection *conn,
83 		     struct buffered_data *in);
84 
85 /* Get global or per domain server features */
86 int do_get_feature(const void *ctx, struct connection *conn,
87 		   struct buffered_data *in);
88 
89 /* Set per domain server features */
90 int do_set_feature(const void *ctx, struct connection *conn,
91 		   struct buffered_data *in);
92 
93 void domain_early_init(void);
94 void domain_init(int evtfd);
95 void init_domains(void);
96 void stubdom_init(bool live_update);
97 void domain_deinit(void);
98 void ignore_connection(struct connection *conn, unsigned int err);
99 
100 /* Returns the implicit path of a connection (only domains have this) */
101 const char *get_implicit_path(const struct connection *conn);
102 
103 /*
104  * Remove node permissions for no longer existing domains.
105  * In case of a change of permissions the related array is reallocated in
106  * order to avoid a data base change when operating on a node directly
107  * referencing the data base contents.
108  */
109 int domain_adjust_node_perms(struct node *node);
110 
111 int domain_alloc_permrefs(struct node_perms *perms);
112 
113 /* Quota manipulation */
114 int domain_nbentry_inc(struct connection *conn, unsigned int domid);
115 int domain_nbentry_dec(struct connection *conn, unsigned int domid);
116 int domain_nbentry_fix(unsigned int domid, int num, bool update);
117 unsigned int domain_nbentry(struct connection *conn);
118 int domain_memory_add(struct connection *conn, unsigned int domid, int mem,
119 		      bool no_quota_check);
120 
121 /*
122  * domain_memory_add_chk(): to be used when memory quota should be checked.
123  * Not to be used when specifying a negative mem value, as lowering the used
124  * memory should always be allowed.
125  */
domain_memory_add_chk(struct connection * conn,unsigned int domid,int mem)126 static inline int domain_memory_add_chk(struct connection *conn,
127 					unsigned int domid, int mem)
128 {
129 	return domain_memory_add(conn, domid, mem, false);
130 }
131 
132 /*
133  * domain_memory_add_nochk(): to be used when memory quota should not be
134  * checked, e.g. when lowering memory usage, or in an error case for undoing
135  * a previous memory adjustment.
136  */
domain_memory_add_nochk(struct connection * conn,unsigned int domid,int mem)137 static inline void domain_memory_add_nochk(struct connection *conn,
138 					   unsigned int domid, int mem)
139 {
140 	domain_memory_add(conn, domid, mem, true);
141 }
142 void domain_watch_inc(struct connection *conn);
143 void domain_watch_dec(struct connection *conn);
144 int domain_watch(struct connection *conn);
145 void domain_outstanding_inc(struct connection *conn);
146 void domain_outstanding_dec(struct connection *conn, unsigned int domid);
147 void domain_transaction_inc(struct connection *conn);
148 void domain_transaction_dec(struct connection *conn);
149 unsigned int domain_transaction_get(struct connection *conn);
150 int domain_get_quota(const void *ctx, struct connection *conn,
151 		     unsigned int domid);
152 
153 /*
154  * Update or check number of nodes per domain at the end of a transaction.
155  * If "update" is true, "chk_quota" is ignored.
156  */
157 int acc_fix_domains(struct list_head *head, bool chk_quota, bool update);
158 void acc_drop(struct connection *conn);
159 void acc_commit(struct connection *conn);
160 int domain_max_global_acc(const void *ctx, struct connection *conn);
161 void domain_reset_global_acc(void);
162 bool domain_max_chk(const struct connection *conn, enum accitem what,
163 		    unsigned int val);
164 
165 extern long wrl_ntransactions;
166 
167 void wrl_check_timeout(struct domain *domain, uint64_t now, int *ptimeout);
168 void wrl_log_periodic(uint64_t now);
169 void wrl_apply_debit_direct(struct connection *conn);
170 void wrl_apply_debit_trans_commit(struct connection *conn);
171 
172 const char *dump_state_connections(FILE *fp);
173 const char *dump_state_domains(FILE *fp);
174 
175 void read_state_connection(const void *ctx, const void *state);
176 void read_state_domain(const void *ctx, const void *state,
177 		       unsigned int version);
178 
179 struct hashtable *domain_check_acc_init(void);
180 void domain_check_acc_add(const struct node *node, struct hashtable *domains);
181 void domain_check_acc(struct hashtable *domains);
182 
183 #endif /* _XENSTORED_DOMAIN_H */
184