datapath.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. /*
  2. * Copyright (c) 2007-2012 Nicira Networks.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <linux/workqueue.h>
  49. #include <net/genetlink.h>
  50. #include "datapath.h"
  51. #include "flow.h"
  52. #include "vport-internal_dev.h"
  53. /**
  54. * DOC: Locking:
  55. *
  56. * Writes to device state (add/remove datapath, port, set operations on vports,
  57. * etc.) are protected by RTNL.
  58. *
  59. * Writes to other state (flow table modifications, set miscellaneous datapath
  60. * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
  61. * genl_mutex.
  62. *
  63. * Reads are protected by RCU.
  64. *
  65. * There are a few special cases (mostly stats) that have their own
  66. * synchronization but they nest under all of above and don't interact with
  67. * each other.
  68. */
  69. /* Global list of datapaths to enable dumping them all out.
  70. * Protected by genl_mutex.
  71. */
  72. static LIST_HEAD(dps);
  73. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  74. static void rehash_flow_table(struct work_struct *work);
  75. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  76. static struct vport *new_vport(const struct vport_parms *);
  77. static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
  78. const struct dp_upcall_info *);
  79. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
  80. const struct dp_upcall_info *);
  81. /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
  82. static struct datapath *get_dp(int dp_ifindex)
  83. {
  84. struct datapath *dp = NULL;
  85. struct net_device *dev;
  86. rcu_read_lock();
  87. dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
  88. if (dev) {
  89. struct vport *vport = ovs_internal_dev_get_vport(dev);
  90. if (vport)
  91. dp = vport->dp;
  92. }
  93. rcu_read_unlock();
  94. return dp;
  95. }
  96. /* Must be called with rcu_read_lock or RTNL lock. */
  97. const char *ovs_dp_name(const struct datapath *dp)
  98. {
  99. struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
  100. return vport->ops->get_name(vport);
  101. }
  102. static int get_dpifindex(struct datapath *dp)
  103. {
  104. struct vport *local;
  105. int ifindex;
  106. rcu_read_lock();
  107. local = rcu_dereference(dp->ports[OVSP_LOCAL]);
  108. if (local)
  109. ifindex = local->ops->get_ifindex(local);
  110. else
  111. ifindex = 0;
  112. rcu_read_unlock();
  113. return ifindex;
  114. }
  115. static void destroy_dp_rcu(struct rcu_head *rcu)
  116. {
  117. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  118. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  119. free_percpu(dp->stats_percpu);
  120. kfree(dp);
  121. }
  122. /* Called with RTNL lock and genl_lock. */
  123. static struct vport *new_vport(const struct vport_parms *parms)
  124. {
  125. struct vport *vport;
  126. vport = ovs_vport_add(parms);
  127. if (!IS_ERR(vport)) {
  128. struct datapath *dp = parms->dp;
  129. rcu_assign_pointer(dp->ports[parms->port_no], vport);
  130. list_add(&vport->node, &dp->port_list);
  131. }
  132. return vport;
  133. }
  134. /* Called with RTNL lock. */
  135. void ovs_dp_detach_port(struct vport *p)
  136. {
  137. ASSERT_RTNL();
  138. /* First drop references to device. */
  139. list_del(&p->node);
  140. rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
  141. /* Then destroy it. */
  142. ovs_vport_del(p);
  143. }
  144. /* Must be called with rcu_read_lock. */
  145. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  146. {
  147. struct datapath *dp = p->dp;
  148. struct sw_flow *flow;
  149. struct dp_stats_percpu *stats;
  150. struct sw_flow_key key;
  151. u64 *stats_counter;
  152. int error;
  153. int key_len;
  154. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  155. /* Extract flow from 'skb' into 'key'. */
  156. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  157. if (unlikely(error)) {
  158. kfree_skb(skb);
  159. return;
  160. }
  161. /* Look up flow. */
  162. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  163. if (unlikely(!flow)) {
  164. struct dp_upcall_info upcall;
  165. upcall.cmd = OVS_PACKET_CMD_MISS;
  166. upcall.key = &key;
  167. upcall.userdata = NULL;
  168. upcall.pid = p->upcall_pid;
  169. ovs_dp_upcall(dp, skb, &upcall);
  170. consume_skb(skb);
  171. stats_counter = &stats->n_missed;
  172. goto out;
  173. }
  174. OVS_CB(skb)->flow = flow;
  175. stats_counter = &stats->n_hit;
  176. ovs_flow_used(OVS_CB(skb)->flow, skb);
  177. ovs_execute_actions(dp, skb);
  178. out:
  179. /* Update datapath statistics. */
  180. u64_stats_update_begin(&stats->sync);
  181. (*stats_counter)++;
  182. u64_stats_update_end(&stats->sync);
  183. }
  184. static struct genl_family dp_packet_genl_family = {
  185. .id = GENL_ID_GENERATE,
  186. .hdrsize = sizeof(struct ovs_header),
  187. .name = OVS_PACKET_FAMILY,
  188. .version = OVS_PACKET_VERSION,
  189. .maxattr = OVS_PACKET_ATTR_MAX
  190. };
  191. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  192. const struct dp_upcall_info *upcall_info)
  193. {
  194. struct dp_stats_percpu *stats;
  195. int dp_ifindex;
  196. int err;
  197. if (upcall_info->pid == 0) {
  198. err = -ENOTCONN;
  199. goto err;
  200. }
  201. dp_ifindex = get_dpifindex(dp);
  202. if (!dp_ifindex) {
  203. err = -ENODEV;
  204. goto err;
  205. }
  206. if (!skb_is_gso(skb))
  207. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  208. else
  209. err = queue_gso_packets(dp_ifindex, skb, upcall_info);
  210. if (err)
  211. goto err;
  212. return 0;
  213. err:
  214. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  215. u64_stats_update_begin(&stats->sync);
  216. stats->n_lost++;
  217. u64_stats_update_end(&stats->sync);
  218. return err;
  219. }
  220. static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
  221. const struct dp_upcall_info *upcall_info)
  222. {
  223. struct dp_upcall_info later_info;
  224. struct sw_flow_key later_key;
  225. struct sk_buff *segs, *nskb;
  226. int err;
  227. segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
  228. if (IS_ERR(skb))
  229. return PTR_ERR(skb);
  230. /* Queue all of the segments. */
  231. skb = segs;
  232. do {
  233. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  234. if (err)
  235. break;
  236. if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
  237. /* The initial flow key extracted by ovs_flow_extract()
  238. * in this case is for a first fragment, so we need to
  239. * properly mark later fragments.
  240. */
  241. later_key = *upcall_info->key;
  242. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  243. later_info = *upcall_info;
  244. later_info.key = &later_key;
  245. upcall_info = &later_info;
  246. }
  247. } while ((skb = skb->next));
  248. /* Free all of the segments. */
  249. skb = segs;
  250. do {
  251. nskb = skb->next;
  252. if (err)
  253. kfree_skb(skb);
  254. else
  255. consume_skb(skb);
  256. } while ((skb = nskb));
  257. return err;
  258. }
  259. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
  260. const struct dp_upcall_info *upcall_info)
  261. {
  262. struct ovs_header *upcall;
  263. struct sk_buff *nskb = NULL;
  264. struct sk_buff *user_skb; /* to be queued to userspace */
  265. struct nlattr *nla;
  266. unsigned int len;
  267. int err;
  268. if (vlan_tx_tag_present(skb)) {
  269. nskb = skb_clone(skb, GFP_ATOMIC);
  270. if (!nskb)
  271. return -ENOMEM;
  272. nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
  273. if (!skb)
  274. return -ENOMEM;
  275. nskb->vlan_tci = 0;
  276. skb = nskb;
  277. }
  278. if (nla_attr_size(skb->len) > USHRT_MAX) {
  279. err = -EFBIG;
  280. goto out;
  281. }
  282. len = sizeof(struct ovs_header);
  283. len += nla_total_size(skb->len);
  284. len += nla_total_size(FLOW_BUFSIZE);
  285. if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
  286. len += nla_total_size(8);
  287. user_skb = genlmsg_new(len, GFP_ATOMIC);
  288. if (!user_skb) {
  289. err = -ENOMEM;
  290. goto out;
  291. }
  292. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  293. 0, upcall_info->cmd);
  294. upcall->dp_ifindex = dp_ifindex;
  295. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  296. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  297. nla_nest_end(user_skb, nla);
  298. if (upcall_info->userdata)
  299. nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
  300. nla_get_u64(upcall_info->userdata));
  301. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  302. skb_copy_and_csum_dev(skb, nla_data(nla));
  303. err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
  304. out:
  305. kfree_skb(nskb);
  306. return err;
  307. }
  308. /* Called with genl_mutex. */
  309. static int flush_flows(int dp_ifindex)
  310. {
  311. struct flow_table *old_table;
  312. struct flow_table *new_table;
  313. struct datapath *dp;
  314. dp = get_dp(dp_ifindex);
  315. if (!dp)
  316. return -ENODEV;
  317. old_table = genl_dereference(dp->table);
  318. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  319. if (!new_table)
  320. return -ENOMEM;
  321. rcu_assign_pointer(dp->table, new_table);
  322. ovs_flow_tbl_deferred_destroy(old_table);
  323. return 0;
  324. }
  325. static int validate_actions(const struct nlattr *attr,
  326. const struct sw_flow_key *key, int depth);
  327. static int validate_sample(const struct nlattr *attr,
  328. const struct sw_flow_key *key, int depth)
  329. {
  330. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  331. const struct nlattr *probability, *actions;
  332. const struct nlattr *a;
  333. int rem;
  334. memset(attrs, 0, sizeof(attrs));
  335. nla_for_each_nested(a, attr, rem) {
  336. int type = nla_type(a);
  337. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  338. return -EINVAL;
  339. attrs[type] = a;
  340. }
  341. if (rem)
  342. return -EINVAL;
  343. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  344. if (!probability || nla_len(probability) != sizeof(u32))
  345. return -EINVAL;
  346. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  347. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  348. return -EINVAL;
  349. return validate_actions(actions, key, depth + 1);
  350. }
  351. static int validate_set(const struct nlattr *a,
  352. const struct sw_flow_key *flow_key)
  353. {
  354. const struct nlattr *ovs_key = nla_data(a);
  355. int key_type = nla_type(ovs_key);
  356. /* There can be only one key in a action */
  357. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  358. return -EINVAL;
  359. if (key_type > OVS_KEY_ATTR_MAX ||
  360. nla_len(ovs_key) != ovs_key_lens[key_type])
  361. return -EINVAL;
  362. switch (key_type) {
  363. const struct ovs_key_ipv4 *ipv4_key;
  364. case OVS_KEY_ATTR_PRIORITY:
  365. case OVS_KEY_ATTR_ETHERNET:
  366. break;
  367. case OVS_KEY_ATTR_IPV4:
  368. if (flow_key->eth.type != htons(ETH_P_IP))
  369. return -EINVAL;
  370. if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
  371. return -EINVAL;
  372. ipv4_key = nla_data(ovs_key);
  373. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  374. return -EINVAL;
  375. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  376. return -EINVAL;
  377. break;
  378. case OVS_KEY_ATTR_TCP:
  379. if (flow_key->ip.proto != IPPROTO_TCP)
  380. return -EINVAL;
  381. if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
  382. return -EINVAL;
  383. break;
  384. case OVS_KEY_ATTR_UDP:
  385. if (flow_key->ip.proto != IPPROTO_UDP)
  386. return -EINVAL;
  387. if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
  388. return -EINVAL;
  389. break;
  390. default:
  391. return -EINVAL;
  392. }
  393. return 0;
  394. }
  395. static int validate_userspace(const struct nlattr *attr)
  396. {
  397. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  398. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  399. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
  400. };
  401. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  402. int error;
  403. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  404. attr, userspace_policy);
  405. if (error)
  406. return error;
  407. if (!a[OVS_USERSPACE_ATTR_PID] ||
  408. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  409. return -EINVAL;
  410. return 0;
  411. }
  412. static int validate_actions(const struct nlattr *attr,
  413. const struct sw_flow_key *key, int depth)
  414. {
  415. const struct nlattr *a;
  416. int rem, err;
  417. if (depth >= SAMPLE_ACTION_DEPTH)
  418. return -EOVERFLOW;
  419. nla_for_each_nested(a, attr, rem) {
  420. /* Expected argument lengths, (u32)-1 for variable length. */
  421. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  422. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  423. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  424. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  425. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  426. [OVS_ACTION_ATTR_SET] = (u32)-1,
  427. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  428. };
  429. const struct ovs_action_push_vlan *vlan;
  430. int type = nla_type(a);
  431. if (type > OVS_ACTION_ATTR_MAX ||
  432. (action_lens[type] != nla_len(a) &&
  433. action_lens[type] != (u32)-1))
  434. return -EINVAL;
  435. switch (type) {
  436. case OVS_ACTION_ATTR_UNSPEC:
  437. return -EINVAL;
  438. case OVS_ACTION_ATTR_USERSPACE:
  439. err = validate_userspace(a);
  440. if (err)
  441. return err;
  442. break;
  443. case OVS_ACTION_ATTR_OUTPUT:
  444. if (nla_get_u32(a) >= DP_MAX_PORTS)
  445. return -EINVAL;
  446. break;
  447. case OVS_ACTION_ATTR_POP_VLAN:
  448. break;
  449. case OVS_ACTION_ATTR_PUSH_VLAN:
  450. vlan = nla_data(a);
  451. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  452. return -EINVAL;
  453. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  454. return -EINVAL;
  455. break;
  456. case OVS_ACTION_ATTR_SET:
  457. err = validate_set(a, key);
  458. if (err)
  459. return err;
  460. break;
  461. case OVS_ACTION_ATTR_SAMPLE:
  462. err = validate_sample(a, key, depth);
  463. if (err)
  464. return err;
  465. break;
  466. default:
  467. return -EINVAL;
  468. }
  469. }
  470. if (rem > 0)
  471. return -EINVAL;
  472. return 0;
  473. }
  474. static void clear_stats(struct sw_flow *flow)
  475. {
  476. flow->used = 0;
  477. flow->tcp_flags = 0;
  478. flow->packet_count = 0;
  479. flow->byte_count = 0;
  480. }
  481. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  482. {
  483. struct ovs_header *ovs_header = info->userhdr;
  484. struct nlattr **a = info->attrs;
  485. struct sw_flow_actions *acts;
  486. struct sk_buff *packet;
  487. struct sw_flow *flow;
  488. struct datapath *dp;
  489. struct ethhdr *eth;
  490. int len;
  491. int err;
  492. int key_len;
  493. err = -EINVAL;
  494. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  495. !a[OVS_PACKET_ATTR_ACTIONS] ||
  496. nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
  497. goto err;
  498. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  499. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  500. err = -ENOMEM;
  501. if (!packet)
  502. goto err;
  503. skb_reserve(packet, NET_IP_ALIGN);
  504. memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
  505. skb_reset_mac_header(packet);
  506. eth = eth_hdr(packet);
  507. /* Normally, setting the skb 'protocol' field would be handled by a
  508. * call to eth_type_trans(), but it assumes there's a sending
  509. * device, which we may not have. */
  510. if (ntohs(eth->h_proto) >= 1536)
  511. packet->protocol = eth->h_proto;
  512. else
  513. packet->protocol = htons(ETH_P_802_2);
  514. /* Build an sw_flow for sending this packet. */
  515. flow = ovs_flow_alloc();
  516. err = PTR_ERR(flow);
  517. if (IS_ERR(flow))
  518. goto err_kfree_skb;
  519. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  520. if (err)
  521. goto err_flow_free;
  522. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  523. &flow->key.phy.in_port,
  524. a[OVS_PACKET_ATTR_KEY]);
  525. if (err)
  526. goto err_flow_free;
  527. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  528. if (err)
  529. goto err_flow_free;
  530. flow->hash = ovs_flow_hash(&flow->key, key_len);
  531. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  532. err = PTR_ERR(acts);
  533. if (IS_ERR(acts))
  534. goto err_flow_free;
  535. rcu_assign_pointer(flow->sf_acts, acts);
  536. OVS_CB(packet)->flow = flow;
  537. packet->priority = flow->key.phy.priority;
  538. rcu_read_lock();
  539. dp = get_dp(ovs_header->dp_ifindex);
  540. err = -ENODEV;
  541. if (!dp)
  542. goto err_unlock;
  543. local_bh_disable();
  544. err = ovs_execute_actions(dp, packet);
  545. local_bh_enable();
  546. rcu_read_unlock();
  547. ovs_flow_free(flow);
  548. return err;
  549. err_unlock:
  550. rcu_read_unlock();
  551. err_flow_free:
  552. ovs_flow_free(flow);
  553. err_kfree_skb:
  554. kfree_skb(packet);
  555. err:
  556. return err;
  557. }
  558. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  559. [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
  560. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  561. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  562. };
  563. static struct genl_ops dp_packet_genl_ops[] = {
  564. { .cmd = OVS_PACKET_CMD_EXECUTE,
  565. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  566. .policy = packet_policy,
  567. .doit = ovs_packet_cmd_execute
  568. }
  569. };
  570. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  571. {
  572. int i;
  573. struct flow_table *table = genl_dereference(dp->table);
  574. stats->n_flows = ovs_flow_tbl_count(table);
  575. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  576. for_each_possible_cpu(i) {
  577. const struct dp_stats_percpu *percpu_stats;
  578. struct dp_stats_percpu local_stats;
  579. unsigned int start;
  580. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  581. do {
  582. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  583. local_stats = *percpu_stats;
  584. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  585. stats->n_hit += local_stats.n_hit;
  586. stats->n_missed += local_stats.n_missed;
  587. stats->n_lost += local_stats.n_lost;
  588. }
  589. }
  590. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  591. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  592. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  593. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  594. };
  595. static struct genl_family dp_flow_genl_family = {
  596. .id = GENL_ID_GENERATE,
  597. .hdrsize = sizeof(struct ovs_header),
  598. .name = OVS_FLOW_FAMILY,
  599. .version = OVS_FLOW_VERSION,
  600. .maxattr = OVS_FLOW_ATTR_MAX
  601. };
  602. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  603. .name = OVS_FLOW_MCGROUP
  604. };
  605. /* Called with genl_lock. */
  606. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  607. struct sk_buff *skb, u32 pid,
  608. u32 seq, u32 flags, u8 cmd)
  609. {
  610. const int skb_orig_len = skb->len;
  611. const struct sw_flow_actions *sf_acts;
  612. struct ovs_flow_stats stats;
  613. struct ovs_header *ovs_header;
  614. struct nlattr *nla;
  615. unsigned long used;
  616. u8 tcp_flags;
  617. int err;
  618. sf_acts = rcu_dereference_protected(flow->sf_acts,
  619. lockdep_genl_is_held());
  620. ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
  621. if (!ovs_header)
  622. return -EMSGSIZE;
  623. ovs_header->dp_ifindex = get_dpifindex(dp);
  624. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  625. if (!nla)
  626. goto nla_put_failure;
  627. err = ovs_flow_to_nlattrs(&flow->key, skb);
  628. if (err)
  629. goto error;
  630. nla_nest_end(skb, nla);
  631. spin_lock_bh(&flow->lock);
  632. used = flow->used;
  633. stats.n_packets = flow->packet_count;
  634. stats.n_bytes = flow->byte_count;
  635. tcp_flags = flow->tcp_flags;
  636. spin_unlock_bh(&flow->lock);
  637. if (used)
  638. NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used));
  639. if (stats.n_packets)
  640. NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
  641. sizeof(struct ovs_flow_stats), &stats);
  642. if (tcp_flags)
  643. NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
  644. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  645. * this is the first flow to be dumped into 'skb'. This is unusual for
  646. * Netlink but individual action lists can be longer than
  647. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  648. * The userspace caller can always fetch the actions separately if it
  649. * really wants them. (Most userspace callers in fact don't care.)
  650. *
  651. * This can only fail for dump operations because the skb is always
  652. * properly sized for single flows.
  653. */
  654. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  655. sf_acts->actions);
  656. if (err < 0 && skb_orig_len)
  657. goto error;
  658. return genlmsg_end(skb, ovs_header);
  659. nla_put_failure:
  660. err = -EMSGSIZE;
  661. error:
  662. genlmsg_cancel(skb, ovs_header);
  663. return err;
  664. }
  665. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  666. {
  667. const struct sw_flow_actions *sf_acts;
  668. int len;
  669. sf_acts = rcu_dereference_protected(flow->sf_acts,
  670. lockdep_genl_is_held());
  671. /* OVS_FLOW_ATTR_KEY */
  672. len = nla_total_size(FLOW_BUFSIZE);
  673. /* OVS_FLOW_ATTR_ACTIONS */
  674. len += nla_total_size(sf_acts->actions_len);
  675. /* OVS_FLOW_ATTR_STATS */
  676. len += nla_total_size(sizeof(struct ovs_flow_stats));
  677. /* OVS_FLOW_ATTR_TCP_FLAGS */
  678. len += nla_total_size(1);
  679. /* OVS_FLOW_ATTR_USED */
  680. len += nla_total_size(8);
  681. len += NLMSG_ALIGN(sizeof(struct ovs_header));
  682. return genlmsg_new(len, GFP_KERNEL);
  683. }
  684. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  685. struct datapath *dp,
  686. u32 pid, u32 seq, u8 cmd)
  687. {
  688. struct sk_buff *skb;
  689. int retval;
  690. skb = ovs_flow_cmd_alloc_info(flow);
  691. if (!skb)
  692. return ERR_PTR(-ENOMEM);
  693. retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
  694. BUG_ON(retval < 0);
  695. return skb;
  696. }
  697. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  698. {
  699. struct nlattr **a = info->attrs;
  700. struct ovs_header *ovs_header = info->userhdr;
  701. struct sw_flow_key key;
  702. struct sw_flow *flow;
  703. struct sk_buff *reply;
  704. struct datapath *dp;
  705. struct flow_table *table;
  706. int error;
  707. int key_len;
  708. /* Extract key. */
  709. error = -EINVAL;
  710. if (!a[OVS_FLOW_ATTR_KEY])
  711. goto error;
  712. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  713. if (error)
  714. goto error;
  715. /* Validate actions. */
  716. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  717. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  718. if (error)
  719. goto error;
  720. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  721. error = -EINVAL;
  722. goto error;
  723. }
  724. dp = get_dp(ovs_header->dp_ifindex);
  725. error = -ENODEV;
  726. if (!dp)
  727. goto error;
  728. table = genl_dereference(dp->table);
  729. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  730. if (!flow) {
  731. struct sw_flow_actions *acts;
  732. /* Bail out if we're not allowed to create a new flow. */
  733. error = -ENOENT;
  734. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  735. goto error;
  736. /* Expand table, if necessary, to make room. */
  737. if (ovs_flow_tbl_need_to_expand(table)) {
  738. struct flow_table *new_table;
  739. new_table = ovs_flow_tbl_expand(table);
  740. if (!IS_ERR(new_table)) {
  741. rcu_assign_pointer(dp->table, new_table);
  742. ovs_flow_tbl_deferred_destroy(table);
  743. table = genl_dereference(dp->table);
  744. }
  745. }
  746. /* Allocate flow. */
  747. flow = ovs_flow_alloc();
  748. if (IS_ERR(flow)) {
  749. error = PTR_ERR(flow);
  750. goto error;
  751. }
  752. flow->key = key;
  753. clear_stats(flow);
  754. /* Obtain actions. */
  755. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  756. error = PTR_ERR(acts);
  757. if (IS_ERR(acts))
  758. goto error_free_flow;
  759. rcu_assign_pointer(flow->sf_acts, acts);
  760. /* Put flow in bucket. */
  761. flow->hash = ovs_flow_hash(&key, key_len);
  762. ovs_flow_tbl_insert(table, flow);
  763. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  764. info->snd_seq,
  765. OVS_FLOW_CMD_NEW);
  766. } else {
  767. /* We found a matching flow. */
  768. struct sw_flow_actions *old_acts;
  769. struct nlattr *acts_attrs;
  770. /* Bail out if we're not allowed to modify an existing flow.
  771. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  772. * because Generic Netlink treats the latter as a dump
  773. * request. We also accept NLM_F_EXCL in case that bug ever
  774. * gets fixed.
  775. */
  776. error = -EEXIST;
  777. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  778. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  779. goto error;
  780. /* Update actions. */
  781. old_acts = rcu_dereference_protected(flow->sf_acts,
  782. lockdep_genl_is_held());
  783. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  784. if (acts_attrs &&
  785. (old_acts->actions_len != nla_len(acts_attrs) ||
  786. memcmp(old_acts->actions, nla_data(acts_attrs),
  787. old_acts->actions_len))) {
  788. struct sw_flow_actions *new_acts;
  789. new_acts = ovs_flow_actions_alloc(acts_attrs);
  790. error = PTR_ERR(new_acts);
  791. if (IS_ERR(new_acts))
  792. goto error;
  793. rcu_assign_pointer(flow->sf_acts, new_acts);
  794. ovs_flow_deferred_free_acts(old_acts);
  795. }
  796. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  797. info->snd_seq, OVS_FLOW_CMD_NEW);
  798. /* Clear stats. */
  799. if (a[OVS_FLOW_ATTR_CLEAR]) {
  800. spin_lock_bh(&flow->lock);
  801. clear_stats(flow);
  802. spin_unlock_bh(&flow->lock);
  803. }
  804. }
  805. if (!IS_ERR(reply))
  806. genl_notify(reply, genl_info_net(info), info->snd_pid,
  807. ovs_dp_flow_multicast_group.id, info->nlhdr,
  808. GFP_KERNEL);
  809. else
  810. netlink_set_err(init_net.genl_sock, 0,
  811. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  812. return 0;
  813. error_free_flow:
  814. ovs_flow_free(flow);
  815. error:
  816. return error;
  817. }
  818. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  819. {
  820. struct nlattr **a = info->attrs;
  821. struct ovs_header *ovs_header = info->userhdr;
  822. struct sw_flow_key key;
  823. struct sk_buff *reply;
  824. struct sw_flow *flow;
  825. struct datapath *dp;
  826. struct flow_table *table;
  827. int err;
  828. int key_len;
  829. if (!a[OVS_FLOW_ATTR_KEY])
  830. return -EINVAL;
  831. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  832. if (err)
  833. return err;
  834. dp = get_dp(ovs_header->dp_ifindex);
  835. if (!dp)
  836. return -ENODEV;
  837. table = genl_dereference(dp->table);
  838. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  839. if (!flow)
  840. return -ENOENT;
  841. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  842. info->snd_seq, OVS_FLOW_CMD_NEW);
  843. if (IS_ERR(reply))
  844. return PTR_ERR(reply);
  845. return genlmsg_reply(reply, info);
  846. }
  847. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  848. {
  849. struct nlattr **a = info->attrs;
  850. struct ovs_header *ovs_header = info->userhdr;
  851. struct sw_flow_key key;
  852. struct sk_buff *reply;
  853. struct sw_flow *flow;
  854. struct datapath *dp;
  855. struct flow_table *table;
  856. int err;
  857. int key_len;
  858. if (!a[OVS_FLOW_ATTR_KEY])
  859. return flush_flows(ovs_header->dp_ifindex);
  860. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  861. if (err)
  862. return err;
  863. dp = get_dp(ovs_header->dp_ifindex);
  864. if (!dp)
  865. return -ENODEV;
  866. table = genl_dereference(dp->table);
  867. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  868. if (!flow)
  869. return -ENOENT;
  870. reply = ovs_flow_cmd_alloc_info(flow);
  871. if (!reply)
  872. return -ENOMEM;
  873. ovs_flow_tbl_remove(table, flow);
  874. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
  875. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  876. BUG_ON(err < 0);
  877. ovs_flow_deferred_free(flow);
  878. genl_notify(reply, genl_info_net(info), info->snd_pid,
  879. ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
  880. return 0;
  881. }
  882. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  883. {
  884. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  885. struct datapath *dp;
  886. struct flow_table *table;
  887. dp = get_dp(ovs_header->dp_ifindex);
  888. if (!dp)
  889. return -ENODEV;
  890. table = genl_dereference(dp->table);
  891. for (;;) {
  892. struct sw_flow *flow;
  893. u32 bucket, obj;
  894. bucket = cb->args[0];
  895. obj = cb->args[1];
  896. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  897. if (!flow)
  898. break;
  899. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  900. NETLINK_CB(cb->skb).pid,
  901. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  902. OVS_FLOW_CMD_NEW) < 0)
  903. break;
  904. cb->args[0] = bucket;
  905. cb->args[1] = obj;
  906. }
  907. return skb->len;
  908. }
  909. static struct genl_ops dp_flow_genl_ops[] = {
  910. { .cmd = OVS_FLOW_CMD_NEW,
  911. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  912. .policy = flow_policy,
  913. .doit = ovs_flow_cmd_new_or_set
  914. },
  915. { .cmd = OVS_FLOW_CMD_DEL,
  916. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  917. .policy = flow_policy,
  918. .doit = ovs_flow_cmd_del
  919. },
  920. { .cmd = OVS_FLOW_CMD_GET,
  921. .flags = 0, /* OK for unprivileged users. */
  922. .policy = flow_policy,
  923. .doit = ovs_flow_cmd_get,
  924. .dumpit = ovs_flow_cmd_dump
  925. },
  926. { .cmd = OVS_FLOW_CMD_SET,
  927. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  928. .policy = flow_policy,
  929. .doit = ovs_flow_cmd_new_or_set,
  930. },
  931. };
  932. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  933. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  934. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  935. };
  936. static struct genl_family dp_datapath_genl_family = {
  937. .id = GENL_ID_GENERATE,
  938. .hdrsize = sizeof(struct ovs_header),
  939. .name = OVS_DATAPATH_FAMILY,
  940. .version = OVS_DATAPATH_VERSION,
  941. .maxattr = OVS_DP_ATTR_MAX
  942. };
  943. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  944. .name = OVS_DATAPATH_MCGROUP
  945. };
  946. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  947. u32 pid, u32 seq, u32 flags, u8 cmd)
  948. {
  949. struct ovs_header *ovs_header;
  950. struct ovs_dp_stats dp_stats;
  951. int err;
  952. ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
  953. flags, cmd);
  954. if (!ovs_header)
  955. goto error;
  956. ovs_header->dp_ifindex = get_dpifindex(dp);
  957. rcu_read_lock();
  958. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  959. rcu_read_unlock();
  960. if (err)
  961. goto nla_put_failure;
  962. get_dp_stats(dp, &dp_stats);
  963. NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
  964. return genlmsg_end(skb, ovs_header);
  965. nla_put_failure:
  966. genlmsg_cancel(skb, ovs_header);
  967. error:
  968. return -EMSGSIZE;
  969. }
  970. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
  971. u32 seq, u8 cmd)
  972. {
  973. struct sk_buff *skb;
  974. int retval;
  975. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  976. if (!skb)
  977. return ERR_PTR(-ENOMEM);
  978. retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
  979. if (retval < 0) {
  980. kfree_skb(skb);
  981. return ERR_PTR(retval);
  982. }
  983. return skb;
  984. }
  985. /* Called with genl_mutex and optionally with RTNL lock also. */
  986. static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
  987. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  988. {
  989. struct datapath *dp;
  990. if (!a[OVS_DP_ATTR_NAME])
  991. dp = get_dp(ovs_header->dp_ifindex);
  992. else {
  993. struct vport *vport;
  994. rcu_read_lock();
  995. vport = ovs_vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
  996. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  997. rcu_read_unlock();
  998. }
  999. return dp ? dp : ERR_PTR(-ENODEV);
  1000. }
  1001. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1002. {
  1003. struct nlattr **a = info->attrs;
  1004. struct vport_parms parms;
  1005. struct sk_buff *reply;
  1006. struct datapath *dp;
  1007. struct vport *vport;
  1008. int err;
  1009. err = -EINVAL;
  1010. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1011. goto err;
  1012. rtnl_lock();
  1013. err = -ENODEV;
  1014. if (!try_module_get(THIS_MODULE))
  1015. goto err_unlock_rtnl;
  1016. err = -ENOMEM;
  1017. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1018. if (dp == NULL)
  1019. goto err_put_module;
  1020. INIT_LIST_HEAD(&dp->port_list);
  1021. /* Allocate table. */
  1022. err = -ENOMEM;
  1023. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1024. if (!dp->table)
  1025. goto err_free_dp;
  1026. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1027. if (!dp->stats_percpu) {
  1028. err = -ENOMEM;
  1029. goto err_destroy_table;
  1030. }
  1031. /* Set up our datapath device. */
  1032. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1033. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1034. parms.options = NULL;
  1035. parms.dp = dp;
  1036. parms.port_no = OVSP_LOCAL;
  1037. parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1038. vport = new_vport(&parms);
  1039. if (IS_ERR(vport)) {
  1040. err = PTR_ERR(vport);
  1041. if (err == -EBUSY)
  1042. err = -EEXIST;
  1043. goto err_destroy_percpu;
  1044. }
  1045. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1046. info->snd_seq, OVS_DP_CMD_NEW);
  1047. err = PTR_ERR(reply);
  1048. if (IS_ERR(reply))
  1049. goto err_destroy_local_port;
  1050. list_add_tail(&dp->list_node, &dps);
  1051. rtnl_unlock();
  1052. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1053. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1054. GFP_KERNEL);
  1055. return 0;
  1056. err_destroy_local_port:
  1057. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1058. err_destroy_percpu:
  1059. free_percpu(dp->stats_percpu);
  1060. err_destroy_table:
  1061. ovs_flow_tbl_destroy(genl_dereference(dp->table));
  1062. err_free_dp:
  1063. kfree(dp);
  1064. err_put_module:
  1065. module_put(THIS_MODULE);
  1066. err_unlock_rtnl:
  1067. rtnl_unlock();
  1068. err:
  1069. return err;
  1070. }
  1071. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1072. {
  1073. struct vport *vport, *next_vport;
  1074. struct sk_buff *reply;
  1075. struct datapath *dp;
  1076. int err;
  1077. rtnl_lock();
  1078. dp = lookup_datapath(info->userhdr, info->attrs);
  1079. err = PTR_ERR(dp);
  1080. if (IS_ERR(dp))
  1081. goto exit_unlock;
  1082. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1083. info->snd_seq, OVS_DP_CMD_DEL);
  1084. err = PTR_ERR(reply);
  1085. if (IS_ERR(reply))
  1086. goto exit_unlock;
  1087. list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
  1088. if (vport->port_no != OVSP_LOCAL)
  1089. ovs_dp_detach_port(vport);
  1090. list_del(&dp->list_node);
  1091. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1092. /* rtnl_unlock() will wait until all the references to devices that
  1093. * are pending unregistration have been dropped. We do it here to
  1094. * ensure that any internal devices (which contain DP pointers) are
  1095. * fully destroyed before freeing the datapath.
  1096. */
  1097. rtnl_unlock();
  1098. call_rcu(&dp->rcu, destroy_dp_rcu);
  1099. module_put(THIS_MODULE);
  1100. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1101. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1102. GFP_KERNEL);
  1103. return 0;
  1104. exit_unlock:
  1105. rtnl_unlock();
  1106. return err;
  1107. }
  1108. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1109. {
  1110. struct sk_buff *reply;
  1111. struct datapath *dp;
  1112. int err;
  1113. dp = lookup_datapath(info->userhdr, info->attrs);
  1114. if (IS_ERR(dp))
  1115. return PTR_ERR(dp);
  1116. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1117. info->snd_seq, OVS_DP_CMD_NEW);
  1118. if (IS_ERR(reply)) {
  1119. err = PTR_ERR(reply);
  1120. netlink_set_err(init_net.genl_sock, 0,
  1121. ovs_dp_datapath_multicast_group.id, err);
  1122. return 0;
  1123. }
  1124. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1125. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1126. GFP_KERNEL);
  1127. return 0;
  1128. }
  1129. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1130. {
  1131. struct sk_buff *reply;
  1132. struct datapath *dp;
  1133. dp = lookup_datapath(info->userhdr, info->attrs);
  1134. if (IS_ERR(dp))
  1135. return PTR_ERR(dp);
  1136. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1137. info->snd_seq, OVS_DP_CMD_NEW);
  1138. if (IS_ERR(reply))
  1139. return PTR_ERR(reply);
  1140. return genlmsg_reply(reply, info);
  1141. }
  1142. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1143. {
  1144. struct datapath *dp;
  1145. int skip = cb->args[0];
  1146. int i = 0;
  1147. list_for_each_entry(dp, &dps, list_node) {
  1148. if (i >= skip &&
  1149. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
  1150. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1151. OVS_DP_CMD_NEW) < 0)
  1152. break;
  1153. i++;
  1154. }
  1155. cb->args[0] = i;
  1156. return skb->len;
  1157. }
  1158. static struct genl_ops dp_datapath_genl_ops[] = {
  1159. { .cmd = OVS_DP_CMD_NEW,
  1160. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1161. .policy = datapath_policy,
  1162. .doit = ovs_dp_cmd_new
  1163. },
  1164. { .cmd = OVS_DP_CMD_DEL,
  1165. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1166. .policy = datapath_policy,
  1167. .doit = ovs_dp_cmd_del
  1168. },
  1169. { .cmd = OVS_DP_CMD_GET,
  1170. .flags = 0, /* OK for unprivileged users. */
  1171. .policy = datapath_policy,
  1172. .doit = ovs_dp_cmd_get,
  1173. .dumpit = ovs_dp_cmd_dump
  1174. },
  1175. { .cmd = OVS_DP_CMD_SET,
  1176. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1177. .policy = datapath_policy,
  1178. .doit = ovs_dp_cmd_set,
  1179. },
  1180. };
  1181. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1182. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1183. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1184. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1185. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1186. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1187. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1188. };
  1189. static struct genl_family dp_vport_genl_family = {
  1190. .id = GENL_ID_GENERATE,
  1191. .hdrsize = sizeof(struct ovs_header),
  1192. .name = OVS_VPORT_FAMILY,
  1193. .version = OVS_VPORT_VERSION,
  1194. .maxattr = OVS_VPORT_ATTR_MAX
  1195. };
  1196. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1197. .name = OVS_VPORT_MCGROUP
  1198. };
  1199. /* Called with RTNL lock or RCU read lock. */
  1200. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1201. u32 pid, u32 seq, u32 flags, u8 cmd)
  1202. {
  1203. struct ovs_header *ovs_header;
  1204. struct ovs_vport_stats vport_stats;
  1205. int err;
  1206. ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
  1207. flags, cmd);
  1208. if (!ovs_header)
  1209. return -EMSGSIZE;
  1210. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1211. NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
  1212. NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
  1213. NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
  1214. NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
  1215. ovs_vport_get_stats(vport, &vport_stats);
  1216. NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1217. &vport_stats);
  1218. err = ovs_vport_get_options(vport, skb);
  1219. if (err == -EMSGSIZE)
  1220. goto error;
  1221. return genlmsg_end(skb, ovs_header);
  1222. nla_put_failure:
  1223. err = -EMSGSIZE;
  1224. error:
  1225. genlmsg_cancel(skb, ovs_header);
  1226. return err;
  1227. }
  1228. /* Called with RTNL lock or RCU read lock. */
  1229. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
  1230. u32 seq, u8 cmd)
  1231. {
  1232. struct sk_buff *skb;
  1233. int retval;
  1234. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1235. if (!skb)
  1236. return ERR_PTR(-ENOMEM);
  1237. retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
  1238. if (retval < 0) {
  1239. kfree_skb(skb);
  1240. return ERR_PTR(retval);
  1241. }
  1242. return skb;
  1243. }
  1244. /* Called with RTNL lock or RCU read lock. */
  1245. static struct vport *lookup_vport(struct ovs_header *ovs_header,
  1246. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1247. {
  1248. struct datapath *dp;
  1249. struct vport *vport;
  1250. if (a[OVS_VPORT_ATTR_NAME]) {
  1251. vport = ovs_vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
  1252. if (!vport)
  1253. return ERR_PTR(-ENODEV);
  1254. if (ovs_header->dp_ifindex &&
  1255. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1256. return ERR_PTR(-ENODEV);
  1257. return vport;
  1258. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1259. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1260. if (port_no >= DP_MAX_PORTS)
  1261. return ERR_PTR(-EFBIG);
  1262. dp = get_dp(ovs_header->dp_ifindex);
  1263. if (!dp)
  1264. return ERR_PTR(-ENODEV);
  1265. vport = rcu_dereference_rtnl(dp->ports[port_no]);
  1266. if (!vport)
  1267. return ERR_PTR(-ENOENT);
  1268. return vport;
  1269. } else
  1270. return ERR_PTR(-EINVAL);
  1271. }
  1272. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1273. {
  1274. struct nlattr **a = info->attrs;
  1275. struct ovs_header *ovs_header = info->userhdr;
  1276. struct vport_parms parms;
  1277. struct sk_buff *reply;
  1278. struct vport *vport;
  1279. struct datapath *dp;
  1280. u32 port_no;
  1281. int err;
  1282. err = -EINVAL;
  1283. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1284. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1285. goto exit;
  1286. rtnl_lock();
  1287. dp = get_dp(ovs_header->dp_ifindex);
  1288. err = -ENODEV;
  1289. if (!dp)
  1290. goto exit_unlock;
  1291. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1292. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1293. err = -EFBIG;
  1294. if (port_no >= DP_MAX_PORTS)
  1295. goto exit_unlock;
  1296. vport = rtnl_dereference(dp->ports[port_no]);
  1297. err = -EBUSY;
  1298. if (vport)
  1299. goto exit_unlock;
  1300. } else {
  1301. for (port_no = 1; ; port_no++) {
  1302. if (port_no >= DP_MAX_PORTS) {
  1303. err = -EFBIG;
  1304. goto exit_unlock;
  1305. }
  1306. vport = rtnl_dereference(dp->ports[port_no]);
  1307. if (!vport)
  1308. break;
  1309. }
  1310. }
  1311. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1312. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1313. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1314. parms.dp = dp;
  1315. parms.port_no = port_no;
  1316. parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1317. vport = new_vport(&parms);
  1318. err = PTR_ERR(vport);
  1319. if (IS_ERR(vport))
  1320. goto exit_unlock;
  1321. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1322. OVS_VPORT_CMD_NEW);
  1323. if (IS_ERR(reply)) {
  1324. err = PTR_ERR(reply);
  1325. ovs_dp_detach_port(vport);
  1326. goto exit_unlock;
  1327. }
  1328. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1329. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1330. exit_unlock:
  1331. rtnl_unlock();
  1332. exit:
  1333. return err;
  1334. }
  1335. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1336. {
  1337. struct nlattr **a = info->attrs;
  1338. struct sk_buff *reply;
  1339. struct vport *vport;
  1340. int err;
  1341. rtnl_lock();
  1342. vport = lookup_vport(info->userhdr, a);
  1343. err = PTR_ERR(vport);
  1344. if (IS_ERR(vport))
  1345. goto exit_unlock;
  1346. err = 0;
  1347. if (a[OVS_VPORT_ATTR_TYPE] &&
  1348. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1349. err = -EINVAL;
  1350. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1351. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1352. if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
  1353. vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1354. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1355. OVS_VPORT_CMD_NEW);
  1356. if (IS_ERR(reply)) {
  1357. netlink_set_err(init_net.genl_sock, 0,
  1358. ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
  1359. goto exit_unlock;
  1360. }
  1361. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1362. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1363. exit_unlock:
  1364. rtnl_unlock();
  1365. return err;
  1366. }
  1367. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1368. {
  1369. struct nlattr **a = info->attrs;
  1370. struct sk_buff *reply;
  1371. struct vport *vport;
  1372. int err;
  1373. rtnl_lock();
  1374. vport = lookup_vport(info->userhdr, a);
  1375. err = PTR_ERR(vport);
  1376. if (IS_ERR(vport))
  1377. goto exit_unlock;
  1378. if (vport->port_no == OVSP_LOCAL) {
  1379. err = -EINVAL;
  1380. goto exit_unlock;
  1381. }
  1382. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1383. OVS_VPORT_CMD_DEL);
  1384. err = PTR_ERR(reply);
  1385. if (IS_ERR(reply))
  1386. goto exit_unlock;
  1387. ovs_dp_detach_port(vport);
  1388. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1389. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1390. exit_unlock:
  1391. rtnl_unlock();
  1392. return err;
  1393. }
  1394. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1395. {
  1396. struct nlattr **a = info->attrs;
  1397. struct ovs_header *ovs_header = info->userhdr;
  1398. struct sk_buff *reply;
  1399. struct vport *vport;
  1400. int err;
  1401. rcu_read_lock();
  1402. vport = lookup_vport(ovs_header, a);
  1403. err = PTR_ERR(vport);
  1404. if (IS_ERR(vport))
  1405. goto exit_unlock;
  1406. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1407. OVS_VPORT_CMD_NEW);
  1408. err = PTR_ERR(reply);
  1409. if (IS_ERR(reply))
  1410. goto exit_unlock;
  1411. rcu_read_unlock();
  1412. return genlmsg_reply(reply, info);
  1413. exit_unlock:
  1414. rcu_read_unlock();
  1415. return err;
  1416. }
  1417. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1418. {
  1419. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1420. struct datapath *dp;
  1421. u32 port_no;
  1422. int retval;
  1423. dp = get_dp(ovs_header->dp_ifindex);
  1424. if (!dp)
  1425. return -ENODEV;
  1426. rcu_read_lock();
  1427. for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
  1428. struct vport *vport;
  1429. vport = rcu_dereference(dp->ports[port_no]);
  1430. if (!vport)
  1431. continue;
  1432. if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
  1433. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1434. OVS_VPORT_CMD_NEW) < 0)
  1435. break;
  1436. }
  1437. rcu_read_unlock();
  1438. cb->args[0] = port_no;
  1439. retval = skb->len;
  1440. return retval;
  1441. }
  1442. static void rehash_flow_table(struct work_struct *work)
  1443. {
  1444. struct datapath *dp;
  1445. genl_lock();
  1446. list_for_each_entry(dp, &dps, list_node) {
  1447. struct flow_table *old_table = genl_dereference(dp->table);
  1448. struct flow_table *new_table;
  1449. new_table = ovs_flow_tbl_rehash(old_table);
  1450. if (!IS_ERR(new_table)) {
  1451. rcu_assign_pointer(dp->table, new_table);
  1452. ovs_flow_tbl_deferred_destroy(old_table);
  1453. }
  1454. }
  1455. genl_unlock();
  1456. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1457. }
  1458. static struct genl_ops dp_vport_genl_ops[] = {
  1459. { .cmd = OVS_VPORT_CMD_NEW,
  1460. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1461. .policy = vport_policy,
  1462. .doit = ovs_vport_cmd_new
  1463. },
  1464. { .cmd = OVS_VPORT_CMD_DEL,
  1465. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1466. .policy = vport_policy,
  1467. .doit = ovs_vport_cmd_del
  1468. },
  1469. { .cmd = OVS_VPORT_CMD_GET,
  1470. .flags = 0, /* OK for unprivileged users. */
  1471. .policy = vport_policy,
  1472. .doit = ovs_vport_cmd_get,
  1473. .dumpit = ovs_vport_cmd_dump
  1474. },
  1475. { .cmd = OVS_VPORT_CMD_SET,
  1476. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1477. .policy = vport_policy,
  1478. .doit = ovs_vport_cmd_set,
  1479. },
  1480. };
  1481. struct genl_family_and_ops {
  1482. struct genl_family *family;
  1483. struct genl_ops *ops;
  1484. int n_ops;
  1485. struct genl_multicast_group *group;
  1486. };
  1487. static const struct genl_family_and_ops dp_genl_families[] = {
  1488. { &dp_datapath_genl_family,
  1489. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1490. &ovs_dp_datapath_multicast_group },
  1491. { &dp_vport_genl_family,
  1492. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1493. &ovs_dp_vport_multicast_group },
  1494. { &dp_flow_genl_family,
  1495. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1496. &ovs_dp_flow_multicast_group },
  1497. { &dp_packet_genl_family,
  1498. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1499. NULL },
  1500. };
  1501. static void dp_unregister_genl(int n_families)
  1502. {
  1503. int i;
  1504. for (i = 0; i < n_families; i++)
  1505. genl_unregister_family(dp_genl_families[i].family);
  1506. }
  1507. static int dp_register_genl(void)
  1508. {
  1509. int n_registered;
  1510. int err;
  1511. int i;
  1512. n_registered = 0;
  1513. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1514. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1515. err = genl_register_family_with_ops(f->family, f->ops,
  1516. f->n_ops);
  1517. if (err)
  1518. goto error;
  1519. n_registered++;
  1520. if (f->group) {
  1521. err = genl_register_mc_group(f->family, f->group);
  1522. if (err)
  1523. goto error;
  1524. }
  1525. }
  1526. return 0;
  1527. error:
  1528. dp_unregister_genl(n_registered);
  1529. return err;
  1530. }
  1531. static int __init dp_init(void)
  1532. {
  1533. struct sk_buff *dummy_skb;
  1534. int err;
  1535. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
  1536. pr_info("Open vSwitch switching datapath\n");
  1537. err = ovs_flow_init();
  1538. if (err)
  1539. goto error;
  1540. err = ovs_vport_init();
  1541. if (err)
  1542. goto error_flow_exit;
  1543. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1544. if (err)
  1545. goto error_vport_exit;
  1546. err = dp_register_genl();
  1547. if (err < 0)
  1548. goto error_unreg_notifier;
  1549. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1550. return 0;
  1551. error_unreg_notifier:
  1552. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1553. error_vport_exit:
  1554. ovs_vport_exit();
  1555. error_flow_exit:
  1556. ovs_flow_exit();
  1557. error:
  1558. return err;
  1559. }
  1560. static void dp_cleanup(void)
  1561. {
  1562. cancel_delayed_work_sync(&rehash_flow_wq);
  1563. rcu_barrier();
  1564. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1565. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1566. ovs_vport_exit();
  1567. ovs_flow_exit();
  1568. }
  1569. module_init(dp_init);
  1570. module_exit(dp_cleanup);
  1571. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1572. MODULE_LICENSE("GPL");