datapath.c 46 KB

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